function log(msg){
    if (window.console && console.log) console.log(msg);
}

function isiPad(){
    return (navigator.platform.indexOf("iPad") != -1);
}

var EG = {
	init: function(){
		$('div#product_images div.thumbs > img').eq(4).css('marginRight', 0);
		EG.addLinks();
	},
	addLinks: function(){
		$('div#product_images div.main > img, div#product_images div.thumbs > img').each(function(){
				$(this).wrap('<a href="'+$(this).next().children().attr('src')+'" rel="gal1" />');
		});
		EG.startColorbox();
	},
	startColorbox: function()
	{
		$("div#product_images a").colorbox({
			close: 'Close Window',
			next: '',
			previous: '',
			current: ''
		});
		
		$("div.video div.item_inner a").colorbox({
			close: 'Close Window',
			next: '',
			previous: '',
			current: '',
			iframe: true,
			innerWidth: "80%", 
			innerHeight: "80%"
		});
	}	
};

Slider = {
	currentImage: 0,
	imageHolders: null,
	imageHolderWidth: null,
	imageCountHolder: null,
	maxCount: null,
	nextButton: null,
	prevButoon: null,
	init: function(imageHolder, imageWrapperWidth, imageCountHolder, nextButton, prevButton, descriptionHolder, descriptionWidth) {
		// set custom variables
		this.imageHolder = imageHolder;
		this.imageWrapperWidth = imageWrapperWidth;
		this.imageCountHolder = imageCountHolder;
		this.maxCount = $("div", imageHolder).length;
		this.nextButton = nextButton;
		this.prevButton = prevButton;
		this.descriptionHolder = descriptionHolder;
		this.descriptionWidth = descriptionWidth;
		this.setCircles(this.maxCount);
		this.updateCount(0);
		this.attachEvents();
	},
	setCircles: function(items) {
		$(this.nextButton).css('visibility', 'visible');
		$(this.prevButton).css('visibility', 'visible');
		var i = 0;
		for(i=0;i<items;i++) {
			if(i<3) {
				$('nav#home_sub_slide_nav').append('<span href="#" class="active"></span>');
			}else{
				$('nav#home_sub_slide_nav').append('<span href="#"></span>');
			}	
		}
	},
	attachEvents: function() {
		// write next/prev functions
		this.nextButton.click(function() {
			Slider.next();
			this.blur();
			return false;
		});
		this.prevButton.click(function() {
			Slider.previous();
			this.blur();
			return false;
		});
		$('nav#home_sub_slide_nav span:not(.active)').live('click', function(){
			if($(this).index() > (Slider.maxCount - 3)){Slider.gotoImage(Slider.maxCount - 3)}else{Slider.gotoImage($(this).index())};
			this.blur();
			return false;
		});
	},
	next: function() {
		// show next image
		this.gotoImage(this.currentImage + 1);
	},
	previous: function() {
		// show previous image
		this.gotoImage(this.currentImage - 1);
	},
	updateCount: function(newCount) {
		// set current image
		$('nav#home_sub_slide_nav span').removeClass('active');
		for(i=newCount;i<newCount+3;i++) {
			$('nav#home_sub_slide_nav span').eq(i).addClass('active');
		}
		this.currentImage = newCount;
	},
	gotoImage: function(num) {
		// if not too high
		if(num + 2 >= this.maxCount) {
			num = 0;
		} else if(num < 0) {
			num = this.maxCount -3;
		}
		//animate
		this.animateContainers(num);
		// update count
		this.updateCount(num);
	},
	animateContainers: function(num) {
		this.imageHolder.animate({
			marginLeft: (num * this.imageWrapperWidth) * -1 + "px"
		}, { duration: 600, queue: false });
	}
};

GiveGoTab = {
	openStatus: null,
	tabDiv: null,
	heightFromTop: null,
	btnBottomHeight: null,
	btnHeight: null,
	imageHeight: null,
	triggerBtn: null,
	openBtnBottom: null,
	closeBtn:null,
	speed: null,
	init: function(tabDiv, heightFromTop, btnBottomHeight, btnHeight, imageHeight, triggerBtn, speed) {
		// set custom variables
		this.openStatus = 0;
		this.tabDiv = tabDiv;
		this.closeBtn = $("#give_go_btn_close");		
		this.heightFromTop = heightFromTop;
		this.btnBottomHeight = btnBottomHeight;
		this.btnHeight = btnHeight;
		this.imageHeight = imageHeight;
		this.triggerBtn = triggerBtn;
		this.openBtnBottom = 0; //triggerBtn.css("bottom");
		this.speed = speed;
		this.tabDivHeight = null;
		this.attachEvents();
	},
	attachEvents: function() {
		// Write Open Close Functions
		this.triggerBtn.click(function() {
			GiveGoTab.toggle();
			this.blur();
		});
		this.closeBtn.click(function(){
			GiveGoTab.toggle();
			this.blur();
		});
	},
	toggle:function(){
		if(GiveGoTab.openStatus){
			GiveGoTab.close();
		}else{
			GiveGoTab.open();
		}
	},
	open: function() {
		// Open GiveGoTab
		var _this = this;
		$("#give_go").animate({
			bottom: 0
		}, 100, null, function(){ 
			//$('#give_go_btn').css('background', 'none');
			_this.triggerBtn.css('display', 'none');
			
			$("#give_go").css('backgroundPosition', 'top center');
			_this.tabDivHeight = $(window).height() - (_this.heightFromTop);
			// log(_this.tabDivHeight);
			// log(_this.heightFromTop)
			_this.animateTab( $(window).height() - (_this.heightFromTop), 800, 'open');
			}
		);
		
	},
	close: function() {
		this.triggerBtn.css('display', 'block');
		//log(this.openBtnBottom);
		this.triggerBtn.css('bottom', this.openBtnBottom - 30 );
		this.tabDiv.css({'position': 'fixed', 'bottom' : this.btnBottomHeight, 'top': 'auto', 'height' : this.tabDivHeight - 75});
		this.animateTab(0, 500, 'close');
	},
	animateTab: function(num, speed, callBack) {
		this.tabDiv.animate({
			height: (num)
		}, { duration: speed, queue: false, complete: function() {GiveGoTab.resetGiveGoTab(callBack)}});
	},
	resetGiveGoTab: function(action) {
		if(action == 'open') {
			this.openStatus = 1;
			this.tabDiv.css({'position': 'absolute', 'top': this.heightFromTop, 'height' : $(document).height() - 110 }); 
		}else if(action == 'close'){
			this.openStatus = 0;
			//$('#give_go_btn').css('background', 'url(/templates/ozito_web/images/framework/btn_give_go2.png) top left');
			//$("#give_go").css('backgroundPosition', '0 200px');
			this.animateTab(24, 100, 'return');
		}else if(action == 'return'){
			//$("#give_go_btn").effect("bounce", { times:5, distance: 20 }, 300);
			this.triggerBtn.animate({
				bottom:0
			},{duration:100});
		}
	}
};

Accordion = {
	accordianItems: null,
	accordianSpeed: null,
	init: function(accordianItems, accordianSpeed) {
		// set custom variables
		this.accordianItems = accordianItems;
		this.accordianSpeed = accordianSpeed;
		// open the first item
		this.open(this.accordianItems[0]);
		//hide video items that aren't populated with any links
		if($("div.video a").length == 0){$("div.video").hide()}
		this.attachEvents();
	},
	attachEvents: function() {
		// Write Open Close Functions
		this.accordianItems.click(function() {
			if($(this).hasClass('open')) {
				Accordion.close(this);
			}else{
				Accordion.close($('div.item h2 span.open'));
				Accordion.open(this);
			}
			return false;
		});
	},
	open: function(element) {
		$(element).addClass('open');
		$(element).parents('div.item').children('div.item_inner').slideDown('slow');
	},
	close: function(element) {
		$(element).removeClass('open');
		$(element).parents('div.item').children('div.item_inner').slideUp('slow');
	}
};

function updateHomeMainFadeNav(){
	$('#home_main_fade_nav span.active').removeClass('active');
	$('#home_main_fade > a').each(function(){
		if($(this).css('opacity') == 1) { $('#home_main_fade_nav span').eq($(this).index()).addClass('active') }
	});
}

$(function() {
	// Only init Home Main Fade if more than 1 image exists. 
	if($("div#home_main_fade img").length > 1) {
		// init Cycle Plugin
		$('#home_main_fade').cycle({  
		    speed:  2000, 
			timeout: 6000,
		    after: updateHomeMainFadeNav 
		});	
		$('#home_main_fade img').each(function(i){
			if(i == 0) { $('#home_main_fade_nav').append('<span href="#" class="active"></span>'); }else{
				$('#home_main_fade_nav').append('<span href="#"></span>');
			}
		});
	}
	// Only init Home Sub Slider if more than 3 images exist
	if($("div#home_sub_slide_wrap div").length > 3) {
		// init gallery: Slider.init(imageHolder, imageWrapperWidth, imageCountHolder, nextButton, prevButton)
		Slider.init($("div#home_sub_slide_wrap"), 315, null, $("a#home_sub_slide_forward"), $("a#home_sub_slide_back"));
	}
	// Clear "Search" text
	$("header form input.text").focus(function(){
		if($(this).val() == "SEARCH"){ $(this).val(""); }
	});
	

	Accordion.init($("section.faqs h2 span"), 2500);
	Accordion.init($("section.product h2 span"), 2500);
	if(!jQuery.browser.mobile) {
		EG.init();
	}
	
	
	var give_go = $('#give_go');
	var give_go_btn = $("#give_go_btn");	

	//format for ipad
	if(isiPad()){
		$("#footer2").css("position","relative");
		give_go_btn.css("display","none");
		give_go.css("display","none");
	}else{
		// Dont Init Give Go Until all images Loaded.
		//function(tabDiv, heightFromTop, btnBottomHeight, btnHeight, imageHeight, triggerBtn, speed)
		give_go.delay(1000).css('visibility', 'visible');
		
		//bounce give go tab only on homepage.
		
		var pathname = window.location.pathname;
		var last = pathname.lastIndexOf("/");
		var page = pathname.slice(last+1);
		if (page === "home" || page === "home.html" ) {
			give_go_btn.css("bottom",-30);
			give_go_btn.delay(1000).animate({
				bottom:0
			},{duration:300});
			//$("#give_go_btn").delay('500').effect("bounce", { times:5, distance: -30 }, 300);
		}		
		
		GiveGoTab.init(give_go, 110, -30, 42, 900, give_go_btn, 1000);
		
	}
	
}); 
