$(document).ready(function(){
	Cufon.replace('#nav a', { hover: {} });
	Cufon.replace('.bucket a.text_box', { hover: {} });
	$('#real_img').animate({'opacity': '0'});
	$('.gallery_over').animate({'opacity': '0'});
	$('#nav a').hover(
		function() {
			var targetX = $(this).offset().left;
			$('#real_img').stop(true, false).animate({'left': targetX + 'px', 'opacity': '1'}, 400, 'easeInOutExpo');
		},
		function() {
			$('#real_img').animate({'opacity': '0'}, 150);
		}
	);
		
	$('.green_arrow').css({'opacity': '0'});
	
	$('.bucket').hover(
		function() {
			$('.green_arrow', this).stop(true, false).animate({'left': '280px', 'opacity': '1'}, 300, 'easeOutExpo');
		}, 
		function() {
			$('.green_arrow', this).animate({'left': '260px', 'opacity': '0'}, 150, 'easeInCubic');
		}
	);
	
	if ($('#gallery_home').length) {
		Shadowbox.init({overlayOpacity: 0.8, onOpen: killCycle, onClose: initCycle});
	
		$(window).bind("resize", resizeGallery);
		resizeGallery();
	}
	
	
	// Tab Navigation on detail page
	if($('#detail_tab').length) {
		$('#detail_tab > ul > li > a').click(function() {
			var targetX = $(this).position()
			var targetoffset = (($(this).width() + 60)/ 2) - 5;
		  	$('#tab_active').animate({'left': ( targetX.left + targetoffset ) + 'px'}, 1000, 'easeInOutExpo');
			//return load_ajax_page( href );
			
		});
	} // end if
	
	// Thumb nail gallery rotator
	if($('#thumb_gallery').length) {
		$("#thumb_gallery").jCarouselLite({
			btnNext: "#thumb_gallery_wrapper .thumb_next",
			btnPrev: "#thumb_gallery_wrapper .thumb_prev"
		});
	}
	
	$('.thumb_gallery_item').hover(
		function(){
			$(this).find('.gallery_over').stop().fadeTo(300, .5);
		},
		function(){
			$(this).find('.gallery_over').stop().fadeTo(300, 0);
		}
	);
	
	
	// Sub Gallery
	if ($('#gallery_wrapper').length) {
		Shadowbox.init({ overlayOpacity: 0.8, onOpen: pauseGallery, onClose: resumeGallery });
		
		initSubCycle();
		
		$(window).bind("resize", resizeSubGallery);
		resizeSubGallery();
	}
	
	/* if($('#video_buckets').length > 0) {
		Shadowbox.setup('#video_buckets .video a', {
			gallery: "video",
			continuous: true
		});
	} */
	
	// align distributor menu
	var offset = ( ($(window).width() -963) / 2 ) - 12;
	
	$('.wrapper963 ul#distributorInfo').css('right', offset + 'px');
});






// Sub Gallery Functions
function pauseGallery() {
	$('#gallery_wrapper').cycle('pause');
}

function resumeGallery() {
	$('#gallery_wrapper').cycle('resume');
}

var subGalleryIndex = 0;

function setCurrentSubGalleryIndex(currElem, nextElem) {
	subGalleryIndex = $(nextElem).index();
	$.logThis('galleryIndex: ' + galleryIndex);
}

function initSubCycle() {
	$('#gallery_wrapper').cycle({
		before:			setCurrentSubGalleryIndex,
		speed:			2000,
		timeout:		7500,
		width:			getWindowWidth(),
		fx:				'fade',
		startingSlide:	subGalleryIndex
	});
}

function resizeSubGallery() {	
	$('#gallery_wrapper, #gallery_wrapper a').css({ 'width': getWindowWidth() });
	initSubCycle();
}

// Details Page, Video/Photo Functions

function play_video(videoHTML) {
	$('#detail_video').html(videoHTML);
}

function show_image(image_url, alt_text) {
	var html = '<img src="'+ image_url +'" width="512" height="288" alt="'+ alt_text +'" />';
	$('#detail_video').fadeOut(1, function() {
		$('#detail_video').html(html);	
		$('#detail_video').fadeIn(500);
	});
}





//
function load_ajax_page( page_url ) {
	$('#detail_load').html('<img src="images/ajax-loader.gif" id="ajax_loader" />');
	// Load the specified URL through AJAX
	$.ajax({
		type : 'POST',
		url : page_url,
		data : {'AJAX_REQUEST':true},
		success : function( data, textStatus, jqXHR ){
			
			$('#detail_load').html( data );

		}
	})
	return false;
} // function ends

function init_sidebar_videos() {
	if($('#video_buckets').length > 0) {
		var hasFlash = swfobject.hasFlashPlayerVersion('9.0.0');
		$('#video_buckets .video').each(function() {
			
			/*
			var href = $(this).find('a').attr('href');
			if(hasFlash) {
				$(this).find('a').attr('href', 'media_player.html?vid=' + href);
			}	
			*/
			// Activates shadowbox (if applicable)
			var title = $(this).find('img').attr('alt');
			
			// Adds Play Button
			var play_html = '<span class="play_btn"></span>';
			$(this).find('a').prepend(play_html);
			
			// Adds Title
			$(this).find('a').append('<span class="title">' + title + ' ></span>');
			Cufon.refresh();	
		});
	}
}

// START HOMEPAGE STUFF
var galleryIndex = 0;
var firstTime = true;

function initCycle() {
	$('#gallery_home').cycle({
		before:			setCurrentGalleryIndex,
		fit:			1,
		speed:			2000,
		timeout:		6000,
		width:			getWindowWidth(),
		height:			getWindowHeight(),
		startingSlide:	galleryIndex,
		fx:				'fade'
	});
}

function killCycle() {
	$('#gallery_home').cycle('destroy');
}

function getWindowWidth() {
	var ss = getScreenSize();
	var w = ss[0];
	//var w = $(window).width();
	if(w < 980) {
		w = 980;	
	}
	return w;
}

function getWindowHeight() {
	var ss = getScreenSize();
	var h = ss[1];
	//var h = $(window).height();
	return h;
}

function getScreenSize() {
	var w = 0;
	var h = 0;
	if(typeof(window.innerWidth) == 'number'){
		w = window.innerWidth;
		h = window.innerHeight;
	}else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	return Array(w,h);
}

function setCurrentGalleryIndex(currElem, nextElem) {
	galleryIndex = $(nextElem).index();
	$.logThis('galleryIndex: ' + galleryIndex);
}

function resizeGallery() {
	var windowHeight = getWindowHeight();
	var targetX = $('#nav').offset().left;
	$('#real_img').css({'left': targetX + 'px', 'opacity': '0'});
	if (!firstTime) {
		$.logThis('!firstTime');
		killCycle();
	} else {
		$.logThis('firstTime');
		firstTime = false;
	}
	
	if(windowHeight < 770) {
		$('#wrapper').css({ 'height': 770 + 'px' });
	} else {
		$('#wrapper').css({ 'height': windowHeight + 'px' });
	}
	
	$('#gallery_home, #gallery_home div').css({ 'width': getWindowWidth(), 'height': getWindowHeight() });
	$('#gallery_home div a').css('cssText', 'width:' + getWindowWidth() + 'px !important;height:' + getWindowHeight() + 'px !important');
	initCycle();
}

jQuery.logThis = function( text ){
	if( (window['console'] !== undefined) ){
		console.log( text );
	}
}
// END HOMEPAGE STUFF
