$(document).ready(function() {
	
	$("#popup1, #popup2, #popup3, #popup4, #popup5, #popup6, #popup7, #popup8").hide();
	
	$("#usage1, #usage2, #usage3, #usage4, #usage5, #usage6, #usage7, #usage8").mouseenter(function(){
		$(this).css("cursor", "pointer");
	});
	
	$("#usage1").click(function(){
		$('#popup1').show();
		$('#popup1').find(".popup-mid").show();		
	});
	$("#usage2").click(function(){
		$('#popup2').show();
		$('#popup2').find(".popup-mid").show();		
	});
	$("#usage3").click(function(){
		$('#popup3').show();
		$('#popup3').find(".popup-mid").show();		
	});
	$("#usage4").click(function(){
		$('#popup4').show();
		$('#popup4').find(".popup-mid").show();		
	});
	$("#usage5").click(function(){
		$('#popup5').show();
		$('#popup5').find(".popup-mid").show();		
	});
	$("#usage6").click(function(){
		$('#popup6').show();
		$('#popup6').find(".popup-mid").show();		
	});
	$("#usage7").click(function(){
		$('#popup7').show();
		$('#popup7').find(".popup-mid").show();		
	});
	$("#usage8").click(function(){
		$('#popup8').show();
		$('#popup8').find(".popup-mid").show();		
	});
	
	$("#popup1, #popup2, #popup3, #popup4, #popup5, #popup6, #popup7, #popup8").click(function(){
		$(this).hide();			
	});
	$(".popup-close").click(function(){
		$(this).parent().hide();			
	});

	// Resizing Browser
	self.moveTo(0,0);
	if ((screen.Width != screen.availWidth) || (screen.Height != screen.availHeight)) { self.resizeTo(screen.availWidth,screen.availHeight)};
	
	//Background image
	$.supersized({
		slides	:  [ { image : 'img/background.jpg' } ]					
	});
	
	
	//
	// Scrollbar Variables
	//
	var iPad = RegExp("AppleWebKit/").test(navigator.userAgent) && RegExp("iPad").test(navigator.userAgent);
	var eduScroll;
	var faqScroll;
	var pane;
	var api;
	if (iPad) {	
		eduScroll = new iScroll('wrapper', { scrollbarClass: 'myScrollbar', hideScrollbar: 'false', momentum: 'true' });
		faqScroll = new iScroll('wrapper2', { scrollbarClass: 'myScrollbar', hideScrollbar: 'false', momentum: 'true' });
	} else {
		$('#wrapper').jScrollPane();
		$('#wrapper2').jScrollPane();
	}


	
	//
	// Accordion
	//
	var accInit = true;
	$("#accordion").accordion({
			changestart: function(event, ui) {								  
				if (accInit) {
					$('#supersized > img').animate({marginTop: "-300px"},{queue:false,duration:300});
				}
				accInit = false;
			}
		});
	$('.accordion .head').click(function() {
			$(this).next().toggle('slow');
			return false;
		}).next().hide();
	
	// Accordion buttons
	$('h3.tab1 a, h3.tab2 a')
		.mouseover(function(){
			if (jQuery.support.opacity) {
					if ($(this).attr('class') != 'selected') $(this).stop().animate({opacity: 0.5},{queue:false,duration:200});
		        } else {
	                if ($(this).attr('class') != 'selected') $(this).css('background-position', '0px -37px' );
		        }
		})
		.mouseout(function(){
			if (jQuery.support.opacity) {
					if ($(this).attr('class') != 'selected') $(this).stop().animate({opacity: 1.0},{queue:false,duration:200});
		        } else {
	                if ($(this).attr('class') != 'selected') $(this).css('background-position', '0px 0px' );
		        }			
		})
		.click(function(){
			initSection($(this).parent().attr('id'));
			if (jQuery.support.opacity) {
					$('h3.tab1 a, h3.tab2 a').removeClass('selected').fadeTo(0, 1);
					$(this).fadeTo(0, 0.5).addClass('selected');
		        } else {
					$('h3.tab1 a, h3.tab2 a').removeClass('selected').css('background-position', '0px 0px' );
	                $(this).css('background-position', '0px -37px' ).addClass('selected');
		        }
		});
		
	function initSection(str) {
				
		$('h3.tab0, h3.tab2').removeClass('tabShadow');
		if (str == "tab1") { 
			if (jQuery.support.opacity) {
					$('h3.tab0').animate({ opacity: 0 }, 0).addClass('tabShadow').animate({ opacity: 1}, 500);
		        } else {
	                $('h3.tab0').delay(1000).addClass('tabShadow');
		        }
			initProducts();
		}
		if (str == "tab2") { 
			if (jQuery.support.opacity) {
					$('h3.tab0').animate({ opacity: 0 }, 0).addClass('tabShadow').animate({ opacity: 1}, 500, function() { setScrollPane(); });
		        } else {
	                $('h3.tab0').delay(1000).addClass('tabShadow').show(0, function() { setScrollPane(); });
		        }
			$('div.tab2').delay(500).addClass('tabShadow');
		}
		if (str == "tab3") { 
			$('div.tab3').addClass('tabShadow');
		}
	}
	
	// Set-up the scroll pane for scroller.js
	function setScrollPane() {
		if (iPad) {	
			setTimeout(function () {						
					eduScroll.refresh();
				}, 100);
		} else {
			pane = $('#wrapper').jScrollPane();
			api = pane.data('jsp');
			api.reinitialise();
		}
	}
	
	// Logo closes accordion
	$('.logo').click(function() {
			$("#accordion").accordion( "activate" , 0 );
			$('#supersized > img').animate({marginTop: "0px"}, 300, function() {
					accInit = true;
				});
			return false;
		});
	
	// Education Link - opens Education section
	$('.educationLink').click(function() {
			$("#accordion").accordion( "activate" , 2 );
			return false;
		});
	
	
	//
	// Product Slider
	//
	var count = 1;
	var current = 0;
	var sliding = true;
	function initProducts() {
		if (jQuery.support.opacity) {
				$("#product"+current).css({ opacity: 0 });
	        } else {
                $("#product"+current).css({ display: 'none' });
	        }		
		productTransition(500);
	}
			
	// Arrow Buttons
	$('.leftArrow, .rightArrow')
		.mouseover(function() {
			$(this).css({backgroundPosition: '0px -71px'});
		})
		.mouseout(function() {
			$(this).css({backgroundPosition: '0px 0px'});
		});
	$('.leftArrow')	
		.click(function(){
			if (sliding){ 
				current = count;
				count --;
				if (count < 1) count = 3; 
				productTransition(0);
			}
		});
	$('.rightArrow')	
		.click(function(){
			if (sliding) {
				current = count;
				count ++;
				if (count > 3) count = 1; 
				productTransition(0);
			}
		});
	$('#productsNav li a').click(function(){
			if (sliding) {
				current = count;
				count = $(this).attr('id').replace("btn", "");
				productTransition(0);
			}
		});
		
	function productTransition(delay) {
		sliding = false;
		jQuery.easing.def = "easeInSine";
		$("*").removeClass('hilite');
		// animate out the current product
		if (jQuery.support.opacity) {
				$("#product"+current+" .productInfo").animate({ opacity: 0 }, 300 ).delay(delay);
				$("#product"+current+" .productImage img").animate({
				    marginLeft: '-=50',
					opacity: 0
				  }, 300, function() {
						// Animation complete.
						$('#product'+current).addClass('visuallyhidden');
						$('#product'+count).removeClass('visuallyhidden');
								$("#product"+count+" .productImage img").css('opacity', '0').css('marginLeft', '575px').animate({
							    	opacity: 1,
									marginLeft: '0'
								  }, 500 );	
						$("#product"+count+" .productInfo").css('opacity', '0').animate({ opacity: 1 }, 500 );
						sliding = true;			
				  }).delay(delay);
	        } else {
                $("#product"+current+" .productInfo").css({ display: 'none' });
				$("#product"+current+" .productImage img").animate({
				    marginLeft: '-=50'
				  }, 300, function() {
						// Animation complete.
						$('#product'+current).addClass('visuallyhidden');
						$('#product'+count).removeClass('visuallyhidden');
			            $("#product"+count+" .productImage img").css('marginLeft', '575px').css({ dispaly: 'block' }).animate({
								marginLeft: '0'
					  		}, 500 );					
				        $("#product"+count+" .productInfo").css({ display: 'block' });
						sliding = true;			
				  }).delay(delay);
	        }
	
		$('#btn'+count).addClass('hilite');
	}
	
	
	/* Education Page*/
	/*
	$('#education1 > .educationMenu').click(function(){
		$('#education1').addClass('visuallyhidden');
		$('#education2').removeClass('visuallyhidden');
		if (iPad) {	
			setTimeout(function () {						
					faqScroll.refresh();
				}, 100);
		} else {
			pane = $('#wrapper2').jScrollPane();
			api = pane.data('jsp');
			api.reinitialise();				
		}	
	});
	$('#education2 > .educationMenu').click(function(){
		$('#education2').addClass('visuallyhidden');
		$('#education1').removeClass('visuallyhidden');
		if (iPad) {	
			setTimeout(function () {						
					eduScroll.refresh();
				}, 100);
		} else {
			pane = $('#wrapper').jScrollPane();
			api = pane.data('jsp');
			api.reinitialise();				
		}	
	});	
	*/
	$('.educationMenu > a.pHOWTOlnk').click(function(){
		$('#education3').addClass('visuallyhidden');
		$('#education2').addClass('visuallyhidden');
		$('#education1').removeClass('visuallyhidden');
		if (iPad) {	
			setTimeout(function () {						
					faqScroll.refresh();
				}, 100);
		} else {
			pane = $('#wrapper').jScrollPane();
			api = pane.data('jsp');
			api.reinitialise();				
		}	
	});
	$('.educationMenu > a.pFAQlnk').click(function(){
		$('#education3').addClass('visuallyhidden');
		$('#education1').addClass('visuallyhidden');
		$('#education2').removeClass('visuallyhidden');
		if (iPad) {	
			setTimeout(function () {						
					eduScroll.refresh();
				}, 100);
		} else {
			pane = $('#wrapper2').jScrollPane();
			api = pane.data('jsp');
			api.reinitialise();				
		}	
	});
	$('.educationMenu > a.pVIDEOlnk').click(function(){
		$('#education2').addClass('visuallyhidden');
		$('#education1').addClass('visuallyhidden');
		$('#education3').removeClass('visuallyhidden');
			/*
			if (iPad) {	
				setTimeout(function () {						
						faqScroll.refresh();
					}, 100);
			} else {
				pane = $('#wrapper3').jScrollPane();
				api = pane.data('jsp');
				api.reinitialise();				
			}
			*/	
	});
	
	
	/* Contact Us Dialog */
	var position = $("#contact").offset();
	$("#contactDialog").dialog({ autoOpen: false, modal: false, zIndex: 3999, position: [(position.left-90), (position.top-240)] });
	$("#contact").click(function(){
		$("#contactDialog").dialog('open');
		return false;
	});
	
	/*  
	var position = $(".usage").offset();
	$(".popup").dialog({ autoOpen: false, modal: false, zIndex: 3999, position: [(position.left-1000), (position.top-0)] });
	$(".usage").click(function(){
		$(".popup").dialog('open');
		return false;
	});
	*/
	
	

	

	
	
});
