jQuery(document).ready(function($) {

	$('form').attr('target','_blank');

	$('.item_add').click(function () {
		if( $('.cart').is(':visible') ) {
			$('.cart').animate({ 'right':0 }, 400, function () {
				$('.cart').delay(1000).animate({ right: -($('.cart').width()-40) });
			});
		} else {
			$('.cart').css({ 'right':0 });
			$('.cart').show();
			$('.cart').delay(1000).animate({ right: -($('.cart').width()-40) });
		}
	});


	$('.simpleCart_empty').click(function () {
		$('.cart').css({ 'right': 0 });
		$('.cart').hide();
	});

	$('.cart').hover(
		function () {
			$(this).stop().animate({ 'right': 0 });
		},
		function () {
			$(this).stop().animate({ right: -($(this).width()-40) });
		}
	);
	
	if($('.int-content-drop').length > 0) {
		$('.int-content-drop').css("height","28px");
		$('.int-content-drop').addClass('int-content-drop-on');
		$('.int-content-drop').click(
			function () {
				if( $(this).height()>28 ) {
					$(this).removeClass('int-content-drop-back');
					$(this).find('big').css('background-position', '0 9px');
					$(this).stop().animate({"height": 28}, 300);
				} else {
					$(this).addClass('int-content-drop-back');
					$(this).find('big').css('background-position', '0 -7px');
					$(this).stop().animate({'height': ( ($(this).find('table').outerHeight() + $(this).find('p').outerHeight()) + 48 ) }, 300);
				};
				
			}
		);
	};


	if($('.int-gallery').length > 0) {
		$('.int-gallery').hide();
		var post_id = $('.int-gallery').attr("rel");
		$('.int-gallery').load('/wp-content/themes/naked-v1/jquery-loads/load-bike-gallery.php', { id:post_id }, function() {
			$(this).fadeIn(400);
		});
		
	
	/*
		SUB CATS REMOVED FOR NOW
		$('.int-gallery-click').click(function () { 
			var get_gallery = $(this).attr("rel");
			$('.int-gallery').fadeOut(200, function() {
				$('.int-gallery').load('/wp-content/themes/naked-v1/load-bike-gallery.php', { id:post_id,  gallery:get_gallery }, function() {
					$('.int-gallery').fadeIn(200);
				});
			});
		});
	*/
	};



	$('.menu-item').hover(
		function () {
			var nav_top = $(this).offset().top;
			var nav_left = $(this).offset().left;
			if($(this).find('ul:first').is(':hidden')) {
				$('a:first', this).addClass('menu-hover-w-sub');
				$(this).find('ul:first').css('top', (nav_top-138));
				$(this).find('ul:first').fadeIn(200);
			};
		},
		function () {
			$('a:first', this).removeClass('menu-hover-w-sub');
			if($('.sub-menu').is(':visible')) {
				$(this).find('ul:first').fadeOut(200);
			};
			
		}
	);

	$('.sub-menu').hover(
		function () {
			$(this).prev('a').css('color', '#B32B00');
		},
		function () {
			$(this).prev('a').css('color', '#333');
		}
	);

	if( $('.bike-picker').length > 0 ) {

		$('.bike-picker td').each(function () {
			//alert( $(this).text() );
			//$('*').replaceAll('<img src="/wp-content/themes/naked-v1/images/picker-star.png">');
			
			stars = '';
			rating = $(this).text();
			if(rating == '*****') {
				stars = '<img src="/wp-content/themes/naked-v1/images/picker-star.png"><img src="/wp-content/themes/naked-v1/images/picker-star.png"><img src="/wp-content/themes/naked-v1/images/picker-star.png"><BR><img src="/wp-content/themes/naked-v1/images/picker-star.png"><img src="/wp-content/themes/naked-v1/images/picker-star.png">';
			} else if(rating == '****') {
				stars = '<img src="/wp-content/themes/naked-v1/images/picker-star.png"><img src="/wp-content/themes/naked-v1/images/picker-star.png"><BR><img src="/wp-content/themes/naked-v1/images/picker-star.png"><img src="/wp-content/themes/naked-v1/images/picker-star.png">';
			} else if(rating == '***') {
				stars = '<img src="/wp-content/themes/naked-v1/images/picker-star.png"><img src="/wp-content/themes/naked-v1/images/picker-star.png"><img src="/wp-content/themes/naked-v1/images/picker-star.png">';
			} else if(rating == '**') {
				stars = '<img src="/wp-content/themes/naked-v1/images/picker-star.png"><img src="/wp-content/themes/naked-v1/images/picker-star.png">';
			} else if(rating == '*') {
				stars = '<img src="/wp-content/themes/naked-v1/images/picker-star.png">';
			}
			if(stars) {
				$(this).html(stars);
			}

		});
	};

	$('.bike-picker td').hover(
		function () {

			stars = '';
			changeColumn = $(this).attr('class');
			changeRow = $(this).parent().attr('class');

			//$('.debug').show();
			getColumnTitle = $('.'+changeColumn).first().text();
			getRowTitle = $(this).parent().text();

			if( changeColumn != 'column-1' && changeRow != 'row-1' ) {
				$('.bike-picker-hover').show();

				$('.bike-picker-hover-arrow').css({ 'left':($(this).outerWidth()/2)+$('.bike-picker-hover-arrow').outerWidth() })
			//	$('.debug').text( 'left: '+$(this).outerWidth() );
				
				$(this).addClass('bike-picker-hovered');
				$(this).parent().addClass('bike-picker-tr-hovered');
				$( '.'+changeColumn ).addClass('bike-picker-td-hovered');
				$(this).removeClass('bike-picker-td-hovered');
				$(this).parent().find('.column-1').addClass('bike-picker-column-1-hover');
				
				$('.bike-picker-hover').css( {'left': $(this).position().left-($(this).width()/4), 'top': $(this).position().top+$(this).height()+10 } );
				rating = $(this).find('img').length;
				if(rating == 5) {
					stars = 'Perfecto';
				} else if(rating == 4) {
					stars = 'Good Choice';
				} else if(rating == 3) {
					stars = 'It will get the job done';
				} else if(rating == 2) {
					stars = 'I suppose you could';
				} else if(rating == 1) {
					stars = 'Forget it';
				}
				
				$('.bike-picker-hover-text').html( getColumnTitle+'<BR>+<BR>'+getRowTitle+'<BR>=<BR>'+stars+'<P><a>Click to view</a>' );
				
			}
		},
		function () {
			$('.bike-picker-hover').hide();
			$(this).removeClass('bike-picker-hovered');
			$(this).parent().removeClass('bike-picker-tr-hovered');
			$( '.'+changeColumn ).removeClass('bike-picker-td-hovered');
			$(this).parent().find('.column-1').removeClass('bike-picker-column-1-hover');
		}
	);


	$('.bike-picker td').click(function () { 
		isActive = $(this).attr('class');
		if( isActive.search( 'bike-picker-hovered' ) > 0 ) {
			getColumnTitle = getColumnTitle.replace(' ', '-');
			getColumnTitle = getColumnTitle.toLowerCase();
			
			window.location = '/the-bikes/'+getColumnTitle;
		}
	});

	if($('.hp-content').length > 0) {
		$('.hp-content').hover(
			function () {
				$('.hp-content-icon', this).stop().animate({"opacity": 1}, 300);
			},
			function () {
				$('.hp-content-icon', this).stop().animate({"opacity": .2}, 300);
			}
		);
	}

// for dropdowns
	
	$(window).resize(function() {
		checkPageHeight();
		if($('.int-content-drop').length > 0) {
			var drop_heights = new Array();
			for (i = 1; i < $('.int-content-drop').length+1; i++) {
				drop_heights['#drop-'+i] = $("#drop-"+i).height();
			};
		};
	});

	/* GALLERY LOAD: */
	if( $('.pages-gallery').length > 0 ) {
		$('.pages-gallery').before('<div class="gallery-loading"></div>');
		$('.pages-gallery').hide();
		
		$('.pages-gallery').each(function(index) {
			$(this).load('/wp-content/themes/naked-v1/jquery-loads/load-gallery.php', { id:$(this).attr("rel") }, function() {
				$(this).prev('.gallery-loading').slideToggle(200, function() {
					$(this).hide();
					$(this).next('.pages-gallery').slideToggle(400);
					if( $('.gallery-img').length == 0 ) {
						$('.post-nav').hide();
					}
				});
			});
		});

	};
	
	if( $('.gallery').length > 0 ) {
		
		$('#gallery').hide();
		var paged =  $('#paged').attr('rel');
		var id =  $('#gallery').attr('rel');
		var get_tag =  '';
		loadMorePhotos();
		
		$('#paged').click(function () { 
			loadMorePhotos()
		});

		$("#tags").change(function() {
			get_tag = $(this).find('option:selected').attr('value');
			paged=0;
			$('#paged').hide();
			$('#gallery').fadeOut(600, function () { 
				$('#gallery').empty();
				$('#gallery').hide();
				loadMorePhotos();
			});
		});

	};


	/* GALLERY LOAD FUNCTION: */
	function loadMorePhotos() {
		paged++;
		
		$.ajax({
			type: 'POST',
			url: '/wp-content/themes/naked-v1/jquery-loads/load-gallery.php',
			data: 'paged='+paged+'&id='+id+'&get_tag='+get_tag,
			cache: false,
			success: function(html){
				if(paged>1) {
					$('#gallery').append(html);
					$('html,body').animate({scrollTop: $("#"+paged).offset().top},400);
				} else {
					$('#gallery').html(html);
					$('#gallery').fadeIn(400);
					
				}
				
				$('#paged').attr('rel',paged);
				if( $('#end').length > 0 ) {
					$('#paged').hide();
				} else {
					$('#paged').show();
				}
		
			}
		});
	};


	/* PAGE LOAD DO: */
	$(window).load(function(){

		if( $('.simpleCart_finalTotal').text() != '$0.00' ) {
			$('.cart').show();
			$('.cart').css({ right: -($('.cart').width()-40) });
		} else {
			$('.cart').hide();
	
		};

		checkPageHeight();

		if($('.hp-photo').length > 0) {
			$('.hp-photo p').css('padding', 20);
			var hp_p_height = $('.hp-photo p').height()+60;
			//alert('hp_p_height: '+(hp_p_height));
			$('.hp-photo p').css('margin-top', -(hp_p_height));
			$('.hp-photo p').fadeIn('slow');
		};
	
		if($('.int-image').length > 0) {
			$('.int-image-content').css('padding', 20);
			var int_img_height = $('#int-img').height();
			$('.int-image').css('height', int_img_height);
			var int_content_height = $('.int-image-content').height()+40;
			//alert('int_content_height: '+int_content_height);
			$('.int-image-content').css('margin-top', -(int_content_height));
			//$('#debug').html('window_height: '+window_height+'<P>window_width: '+window_width+'<P>page_height: '+page_height+'<P>page_width: '+page_width+'<P>page_position: '+page_position+'<P>backrightWidth: '+backrightWidth+'<P>pageRightPosition: '+pageRightPosition+'<P>pageRightWidth: '+pageRightWidth);
			$('.int-image-content').fadeIn('slow');
		};

	});

	function checkPageHeight() {
		var window_height = $(window).height();
		var window_width = $(window).width();
	
	
		var page_height = $('.page').height();
		var page_width = $('.page').width();
		var page_position = $('.page').offset().left;
	
	
		var backrightWidth = $('.back-right').width();
		
		var pageRightPosition = (page_width + page_position)-40;
		var pageRightWidth = (window_width - pageRightPosition);

		
		if(pageRightWidth < 210) {
			$('.back-right').width(pageRightWidth)
		};
		
		if(page_height < window_height) {
			$('.back-left').height(window_height)
			$('.back-right').height(window_height)
		};
	
		$('.back-right').css('left', pageRightPosition)
	
		if(page_height < window_height) {
			$('.footer').show();	
		} else {
			$('.footer').hide();	
		};

		$('.back-left').fadeIn('slow');
		$('.back-right').fadeIn('slow');
	};

	counter = 0;
	$('.post').live('click', function(){
		window.location = $(this).attr('rel');
	});

	$('.view-bike-gallery').live('click',function () {
		$('.extras a').hide();
		post_ID = $(this).attr("rel");
		if( $(this).hasClass('int-gallery-full') ) {
			full_size = 1;
		} else {
			full_size = 0;
		}
		
		$('.int-content-images').html('<div class="gallery-loading int-gallery-load"></div>');
		$.post('/wp-content/themes/naked-v1/jquery-loads/load-gallery.php', { 'id':post_ID, 'full_size':full_size }, function(returnHTML) {
			$('.int-content-images').fadeOut(400, function() {
				$('.int-content-images').html(returnHTML);
				$('.int-content-images').fadeIn(400);
				$('.extras').html('<a class="extras-reset">reset</a></div>');
			});
		});
	});

	$('.extras-reset').live('click',function () {
		$('.extras a').hide();
		post_id = $('.int-gallery').attr("rel");
		$('.int-content-images').html('<div class="gallery-loading int-gallery-load"></div>');
		$.post('/wp-content/themes/naked-v1/jquery-loads/load-bike-gallery.php', { id:post_id, 'full_size':full_size }, function(returnHTML) {
				$('.int-content-images').fadeOut(400, function() {
					$('.int-gallery').html(returnHTML);
				});
		});
	});

});



