// vars for AddThis
var addthis_pub="xa-4a80838d505e9dce";
var addthis_options = 'delicious, digg, email, facebook, favorites, friendfeed, google, linkedin, myspace, print, reddit, stumbleupon, technorati, twitter';
var relatedListHeight = 0;






$(document).ready(function() {
						   
	// ################## Basic Jquery operations ##################

    // rollovers
    PEPS.rollover.init();

    // PNG tranparency for IE6
    $('.png').pngfix();

    // set div links
    $(".link").click(function() {
        window.location = $(this).find("a").attr("href");
        return false;
    });
	
	$('a.current').click(function(){
		return false;							  
	});

    // external links
    $("a[rel='external']").attr('target','_blank');

    // Add end class to list items and table rows
    $('li:last','ul').addClass('end');
    $('tr:last','table').addClass('end');
	
	// CSS zoom fix for IE6 hasLayout bugs
	if($.browser.msie && parseInt($.browser.version) == 6){
		$('*').css({'zoom' : '1'});	
	}
	
	
	// ################## Site Specific operations ##################
	
	// Homepage Slideshow
	$('#slideshowNavBg').fadeTo(0,0.66);
	
	$('#homeSlideshow ul').cycle({ 
    fx:     'fade', 
    speed:  600, 
    timeout: 4000, 
    pager:  '#slideshowNavButtons' 
	});
	
	// Featured Products Ajax
	
	//Nav buttons
	
	$('#featuredProductsLeftArrow').click(function(){
		if($(this).hasClass('disabled')== false){
			featuredProductsCurrentPage--;
			getNextFeaturedProductsPage();
		}
		if(featuredProductsCurrentPage == 1) {
			$(this).addClass('disabled').removeClass('hover');	
		}
		if(featuredProductsCurrentPage < featuredProductsPages) {
			$('#featuredProductsRightArrow').removeClass('disabled');	
		}
	});
	
	$('#featuredProductsRightArrow').click(function(){
		if($(this).hasClass('disabled')== false){
			featuredProductsCurrentPage++;
			getNextFeaturedProductsPage();
		}
		if(featuredProductsCurrentPage == featuredProductsPages) {
			$(this).addClass('disabled').removeClass('hover');	
		}
		if(featuredProductsCurrentPage > 1) {
			$('#featuredProductsLeftArrow').removeClass('disabled');	
		}
	});
	
	$('#featuredProductsLeftArrow, #featuredProductsRightArrow').hover(function(){
			if($(this).hasClass('disabled') == false){
				$(this).addClass('hover');
			}
	},function(){
				$(this).removeClass('hover');
	});
	
	
	// ############## Add To My Bag ##########################
	$('.addToCart').live('click', function(){
		var what = $(this).attr('id');
		addToMyBag(what);
		return false;
	});
	
	// ############## Adjust leftSubNav background positioning for IE ##########################
	if($.browser.msie){
		$('.leftSubNav ul li ul li a').css({'background' : 'url(css/img/bg_leftSubMenu_dot.png) no-repeat 7px 10px'});
		$('.leftSubNav ul li ul li ul li a').css({'background' : 'none'});
	}
	
	// ############## ADD THIS ##########################
	$('a.viralShare1, #videoTitleBox a').hover(function(){
		return addthis_open(this, '', '[URL]', '[TITLE]');
	}, function(){
		return addthis_close();
	}).click(function(){
		return addthis_sendto();
	});
	
	// ############## PRESCRIPTION DEMO ##########################
	
	if ( $("#stepsHolder").length > 0 ) {
		$('#step0').addClass('currentB');
		$('#step0:not(.currentB)').live('click', function(event){
			event.preventDefault();
			$('#stepsHolder2').animate({marginLeft: '0px'}, 500, function(){
			$('a','#prescriptionsStepsNav p').removeClass('currentB');
			$('#step0').addClass('currentB');
			});					   
		});
		$('#step1:not(.currentB), #startDemoButton').live('click', function(event){
			event.preventDefault();
			$('#stepsHolder2').animate({marginLeft: '-900px'}, 500, function(){
			$('a','#prescriptionsStepsNav p').removeClass('currentB');
			$('#step1').addClass('currentB');
			});					   
		});
		$('#step2:not(.currentB), #step1NextLink').live('click', function(event){
			event.preventDefault();
			$('#stepsHolder2').animate({marginLeft: '-1800px'}, 500, function(){
			$('a','#prescriptionsStepsNav p').removeClass('currentB');
			$('#step2').addClass('currentB');
			});					   
		});
		$('#step3:not(.currentB), #step2NextLink').live('click', function(event){
			event.preventDefault();
			$('#stepsHolder2').animate({marginLeft: '-2700px'}, 500, function(){
			$('a','#prescriptionsStepsNav p').removeClass('currentB');
			$('#step3').addClass('currentB');
			});					   
		});
		
	}
	
	
	// ################## Contact Form ##################
	
	$('#contactSubmitButton').hover(function(){
		$(this).addClass('hover');
	},function(){
		 $(this).removeClass('hover');
	});
	
	
	// ################## PPTV Player ##################\
	
	
	if ( $("#videoList").length > 0 ){
		$('li a', '#videoList').click(function(){
			$(this).closest('li').click();
			return false;
		});
		if(document.location.hash){
			var urlHash = document.location.hash;
			urlHash = urlHash.substring(1);
			$('li a', '#videoList').each(function(){
				if($(this).attr('rel') == urlHash){
					$('li', '#videoList').removeClass('current');
					$(this).closest('li').addClass('current');
					var videoURL = $(this).attr('href');
					var vtitle = $(this).siblings('h3').text();
					var vdate = $(this).siblings('h4').text();
					var temp = new Array();
					temp = videoURL.split('?v=');
					video = temp[1];
					getYouTubeMovie(video);
					document.location.hash = urlHash;
					$('#videoTitleBox h1').text(vtitle);
					$('#videoTitleBox h2').text(vdate);
					return false;
				}else{
					showDefaultMovie();	
				}
			});	
		}else{
			showDefaultMovie();	
		}
		
		$('li', '#videoList').click(function(){
			if($(this).hasClass('current')==false){
				$('li', '#videoList').each( function(){
					$(this).removeClass('current');
				});
				$(this).addClass('current');
				$(this).removeClass('hover');
				var videoURL = $(this).find('a').attr('href');
				var vtitle = $(this).find('h3').text();
				var vdate = $(this).find('h4').text();
				var temp = new Array();
				temp = videoURL.split('?v=');
				video = temp[1];
				getYouTubeMovie(video);
				document.location.hash = $(this).find('a').attr('rel');
				$('#videoTitleBox h1').text(vtitle);
				$('#videoTitleBox h2').text(vdate);
			}		
		});
	
	
		$('li','#videoList').live('mouseover', function(){
			var current = $(this).hasClass('current');
			if(current ==false){
				$(this).addClass('hover');
			}
		});
		$('li','#videoList').live('mouseout', function(){
			var current = $(this).hasClass('current');
			if(current ==false){
				$(this).removeClass('hover');
			}
		});
	}
	
	// ################## Add to Shopping Bag Details Button ##################
	
	$('.detailsInfo h4 input').hover(function(){
		var disabledButton = $(this).hasClass('disabled');
		if(disabledButton == false){
			$(this).addClass('hover');
		}
	}, function(){
		var disabledButton = $(this).hasClass('disabled');
		if(disabledButton == false){
			$(this).removeClass('hover');
		}
	});
	
	
	
});


// ################## Site Specific Functions ##################

function getNextFeaturedProductsPage(){
	//this calls the php file using a post, sending the bracketed parameters as POST variables
	$('#featuredProducts ul').fadeTo('fast',0);
	$('#featuredProducts ul').load('data/featuredProducts.php', {currentPage: featuredProductsCurrentPage}, function(){
   		$('#featuredProducts ul').fadeTo('fast',0.99);
 	});

}

function addToMyBag(what){
	var total = parseInt($('#shoppingBagCount a').text());
	var whatProduct = what;
	var rnum=Math.floor(Math.random()*11);
	$('#shoppingBagCount a').load('data/addToMyBag.php?r='+rnum, {currentTotal: total, productID: whatProduct }, function(){
   		if($('#topBag').hasClass('empty') == true){
			$('#topBag').removeClass('empty');	
		}
 	});
}

function getYouTubeMovie(thismovie) {
	var youtubeVideo = "<object width=\"100%\" height=\"100%\"><param name=\"movie\" value=\"http://www.youtube.com/v/" + thismovie + "?showinfo=0\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><param name=\"showinfo\" value=\"0\"><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/" + thismovie + "?showinfo=0\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" wmode=\"transparent\" showinfo=\"0\" width=\"100%\" height=\"100%\"></embed></object>"
	$('#videoHolder').html(youtubeVideo);


}

function showDefaultMovie(){
		$('li:first','#videoList').addClass('current');
		var videoURL = $('li:first a', '#videoList').attr('href');
		var vtitle = $('li:first h3', '#videoList').text();
		var vdate = $('li:first h4', '#videoList').text();
		var temp = new Array();
		temp = videoURL.split('?v=');
		video = temp[1];
		getYouTubeMovie(video);
		document.location.hash = $('li:first a','#videoList').attr('rel');
		$('#videoTitleBox h1').text(vtitle);
		$('#videoTitleBox h2').text(vdate);
}