// BikeCaffe Needed jQuery Scripts

var tabbed_divs = [ 'Twitter', 'Facebook', 'Myspace', 'Flickr' ];
var product_index = new Array();
//var shopping_cart = new Array();

var EMPTY_CART = "<p>Your cart is currently empty. Please choose some products from the left to add to your cart.</p>";


function hide_social_divs()
{
	for( index = 0; index < tabbed_divs.length; index++ )
	{
		div_id = "#" + tabbed_divs[ index ] + "Main";

		if( $( div_id ).css( 'display' ) != 'none' )
			$( div_id ).stop( false, true ).animate( {opacity: 0}, 250, 'swing', function() {
				$( this ).css( { display: 'none' } );
			} );
	}
}

$(document).ready(function() {
	// Side Promo Active State
	$( ".SidePromo" ).mouseover(
		function() {
			$(this).children( "div.SidePromoHeader" ).stop().animate( {backgroundPosition: "-254px 0"}, {duration:0} );
		}
	).mouseout(
		function() {
			$(this).children( "div.SidePromoHeader" ).stop().animate( {backgroundPosition: "0 0"}, {duration:0} );
		}
	);



	$( "#TwitterTab" ).mouseover(
		function() {
			if( $( "#TwitterMain" ).css( 'display' ) == 'none' )
			{
				hide_social_divs();
				$( "#TwitterMain" ).stop( false, true ).css( {display: '', opacity: 0 } ).animate( {opacity: 100}, {duration: 1000}, 'swing' );
			}
		} );
	$( "#FacebookTab" ).mouseover(
		function() {
			if( $( "#FacebookMain" ).css( 'display' ) == 'none' )
			{
				hide_social_divs();
				$( "#FacebookMain" ).stop( false, true ).css( {display: '', opacity: 0 } ).animate( {opacity: 100}, {duration: 1000}, 'swing' );
			}
		} );
	$( "#MyspaceTab" ).mouseover(
		function() {
			if( $( "#MyspaceMain" ).css( 'display' ) == 'none' )
			{
				hide_social_divs();
				$( "#MyspaceMain" ).stop( false, true ).css( {display: '', opacity: 0 } ).animate( {opacity: 100}, {duration: 1000}, 'swing' );
			}
		} );
	$( "#FlickrTab" ).mouseover(
		function() {
			if( $( "#FlickrMain" ).css( 'display' ) == 'none' )
			{
				hide_social_divs();
				$( "#FlickrMain" ).stop( false, true ).css( {display: '', opacity: 0 } ).animate( {opacity: 100}, {duration: 1000}, 'swing' );
			}
		} );



	$("#Carousel").jCarouselLite({
		visible: 2,
		btnNext: "#FlickrRightButton",
		btnPrev: "#FlickrLeftButton"
	});

	$( "#FlickrMain" ).css( {display: 'none'} );



	// Sliding with Browser Div

	$(window).scroll(function() {
		cartScroll();
	});


	// Adding products to the cart...
	
	var index = 0;
	$('.ProductTable input[type="text"]').each(function() {
		$(this).attr( "id", "co_" + index );
		var children = $(this).parent().parent().children();
		
		var title = ((children[0].innerHTML.replace(/^\s+|\s+$/g,"") != "" && children[0].innerHTML.replace(/^\s+|\s+$/g,"") != "&nbsp;" ) ? children[0].innerHTML + ( (children[1].innerHTML.replace(/^\s+|\s+$/g,"") != "" && children[1].innerHTML.replace(/^\s+|\s+$/g,"") != "&nbsp;" ) ? " (" + children[1].innerHTML.replace( "<br>","") + ")" : ""  ) : children[1].innerHTML).replace("<br>","");
		
		var mark;
		
		if( children[ 0 ].colSpan == 2 )
			var price_m = children[1].innerHTML;
		else
			var price_m = children[2].innerHTML;
		
		if( (mark = price_m.search( / or /i )) >= 0 ) {
			price = price_m.substring( 1, price_m.substring( 1, mark ).search( / / ) + 1 );
			price2 = price_m.substring( mark + 5, price_m.substring( mark + 5 ).search( / for /i ) + mark + 5 );
			count = price_m.substring( mark + 5 + price_m.substring( mark + 5 ).search( / for /i ) + 5 );
		} else if( (mark = price_m.search( / \/ / )) >= 0 ) {
			price = price_m.substring( 1, mark + 1 );
			price2 = null;
			count = null;
		} else {
			price = price_m.substring( 1 );
			price2 = null;
			count = null;
		}
		
		product_index[ index ] = [ title, price, price2, count ];
		index++;
	});

	$('.ProductTable input[type="text"]').change( function() {
		updateProductList();
		updateCart();
	});
	
	$('.ProductTable input[type="text"]').keyup( function() {
		updateProductList();
		updateCart();
	});

	clearCart();

	updateProductList();
});

function cartScroll() {
	$('.Sliding').css( { position: 'absolute' } );

	var top = parseInt( $('.Sliding').css( 'top' ).substring( 0, $('.Sliding').css( 'top' ).length - 2 ) );
	var height = parseInt( $('.Sliding').css('height').substring( 0, $('.Sliding').css('height').length - 2 ) );
	var scrollTop = $(window).scrollTop();

	if( $(window).height() < 600 ) {
		if( height > 200 ) {
			$('#Products').css( 'height', '200px' );
			$('#Products').css( 'max-height', '200px' );
		}
	} else {
		if( height > 500 ) {
			$('#Products').css( 'height', '500px' );
			$('#Products').css( 'max-height', '500px' );
		}
	}

	if( scrollTop == 0 ) {
		$('.Sliding').stop( true, false).animate( {
						left: 20,
						top: 35
		}, 250, 'swing' );
	} else if( top < scrollTop - 144 ) {
		var content_height = parseInt( $('#Content').css( 'height' ).substring( 0, $('#Content').css( 'height' ).length - 2 ) );
		var content_top = parseInt( $('#Content').css( 'top' ).substring( 0, $('#Content').css( 'top' ).length - 2 ) );

		$('.Sliding').stop( true, false).animate( {
			left: 20,
			top: ( ( scrollTop + height > content_height + content_top ) ? ( content_top + content_height - height ) : ( height + 300 > $(window).height() ? scrollTop - 100 : scrollTop ) )
		}, 250, 'swing' );
	} else if( (top + height + 200) > (scrollTop + $(window).height()) ) {
		$('.Sliding').stop( true, false).animate( {
			left: 20,
			top: ( ( scrollTop + $(window).height() - height - 300 ) < 35 ? 35 : ( scrollTop + $(window).height() - height - 300 < scrollTop ? scrollTop - 100 : scrollTop + $(window).height() - height - 300 ) )
		}, 250, 'swing' );
	}
}

function deleteProd( prod_index ) {
	document.getElementById( 'co_' + prod_index ).value = "";
	updateCart();
}

function updateCart() {
	shopping_cart = new Array();
	
	$('.ProductTable input[type="text"]').each(function() {
		if( $(this).attr( 'value' ) != "" && $(this).attr( 'value' ) > 0 )
			shopping_cart.push( [ $(this).attr( 'id' ).substring(3), $(this).attr( 'value' ) ] );
	});
		
	var total_price = 0;
	
	if( shopping_cart.length > 0 ) {
		var prod_html = "<table width='100%'>";

		for( var index = 0; index < shopping_cart.length; index++ ) {
			var prod_index = shopping_cart[ index ][ 0 ];
			var prod_count = shopping_cart[ index ][ 1 ];

			var prod = product_index[ prod_index ];

			if( prod[ 2 ] === null ) {
				var price = prod[ 1 ] * prod_count;
			} else {
				var price = prod[ 2 ] * Math.floor( prod_count / prod[ 3 ] ) + prod[ 1 ] * ( prod_count % prod[ 3 ] );
			}

			prod_html += "<tr style='border-bottom-style: inset;'><td>" + prod[ 0 ] + ( prod_count > 1 ? ' (x' + prod_count + ')' : '' ) + "</td><td align='right'><strong>$" + price.toFixed(2) + "</strong></td></tr>";
			total_price += price;
		}

		prod_html += "</table>";
	} else var prod_html = EMPTY_CART;
	
	$('#Products').html( prod_html )
	$('#Items').html( shopping_cart.length + " Item" + ( shopping_cart.length != 1 ? 's' : '' ) );
	$('#Price').html( '$' + total_price.toFixed(2) );
	
	cartScroll();
}

function updateProductList() {
	var header_array = new Array();

	var show = 'none';

	$('.ProductTable h2').each( function() {
		if( $(this).parent().parent().hasClass( 'toggleOn' ) ) {
			$(this).html( $(this).html().replace( /\[\+\]/, "[-]" ) );
			show = "";
		} else {
			if( !$(this).parent().parent().hasClass( 'toggleOff' ) )
			{
				$(this).css( 'cursor', 'pointer' );
				$(this).css( 'text-decoration', 'underline' );
				$(this).parent().parent().addClass( 'toggleOff' );
				$(this).html( "[+] " + $(this).html() );
				$(this).click( function() { $(this).parent().parent().toggleClass( "toggleOn toggleOff");updateProductList(); } );
			}
			else	$(this).html( $(this).html().replace( /\[-\]/, "[+]" ) );
			show = 'none';
		}

		var item_count = 0;
		
		$(this).parent().parent().nextAll().each( function() {
			if( $(this).find('h2').length > 0 )
				return false;

			if( $(this).find('input[type="text"]').attr('value') > 0 )
				item_count++;

			$(this).css( 'display', show );
		});

		$(this).html( $(this).html().replace( / \( [0-9]+ Item[s]* \)$/, "" ) );

		if( item_count > 1 )
		{
			$(this).html( $(this).html() + " ( " + item_count + " Items )" );
		} else if( item_count > 0 ) {
			$(this).html( $(this).html() + " ( " + item_count + " Items )" );
		}
	});
}

function submitCart() {
	cart_output = "\n";
	
	$('.ProductTable input[type="text"]').each( function() {
		if( parseInt( $(this).attr('value') ) > 0 ) {

			var title = ( $(this).parent().parent().children().first().html().replace(/^\s+|\s+$/g,"") != "" && $(this).parent().parent().children().first().html().replace(/^\s+|\s+$/g,"") != "&nbsp;" ) ? $(this).parent().parent().children().first().html().replace('<br>','').replace('&nbsp;', '') + ( ( $(this).parent().parent().children(':nth-child(2)').html().replace(/^\s+|\s+$/g,"") != "" && $(this).parent().parent().children(':nth-child(2)').html().replace(/^\s+|\s+$/g,"") != "&nbsp;" ) ? " (" + $(this).parent().parent().children(':nth-child(2)').html().replace( "<br>","").replace('&nbsp;') + ")" : ""  ) : $(this).parent().parent().children().first().next().html().replace('<br>','').replace('&nbsp;');

			cart_output += $(this).attr("value") + ":\t" + title + "\n";
		}
	});
	
	$('#cart_output').attr( "value", cart_output );

	return true;
}

function clearCart() {
	$('.ProductTable input[type="text"]').each( function() {
		$(this).attr("value", "");
	});
}

