$(function() {

    $('#search input.searchfield').focus(function() {
        $(this).css('background-position','200px');
    }); 	
    $('#search input.searchfield').blur(function() {
		if($(this).val() == "") {
        	$(this).css('background-position','0');
		}
    }); 	
    $('#searchBattery').focus(function() {
        $(this).val('');
    }); 
    $('#searchBattery').blur(function() {
		if($(this).val() == "") {
        	$(this).val('search by battery or device');
		}
    });	
    $('#battery_search_box .battery_search').focus(function() {
        $(this).val('');
    }); 
    $('#battery_search_box .battery_search').blur(function() {
		if($(this).val() == "") {
        	$(this).val('Search by Battery or Device Model');
		}
    });		
	
	/* NAV DROP DOWN */			   
	$("#nav > ul > li").hover(function() {
		$(this).find('div:first').show();
		$(this).find('a:first').css('background-position', '0 -31px');
	}, function() {
		$(this).find('div:first').hide();
		$(this).find('a:first').css('background-position', '0 0');
		$(this).find('a:#navSelect').css('background-position', 'bottom');
	});	
	
	// hide sub images
	$("#nav > ul > li > div > a").find('img:eq(1)').hide();
	$("#nav > ul > li#nav_products > div > a").hover(function() {
		$(this).find('img:eq(0)').hide();		 
		$(this).find('img:eq(1)').show();		
	}, function() {
		$(this).find('img:eq(1)').hide();	
		$(this).find('img:eq(0)').show();
	});	
	$("#nav > ul > li#nav_battery > div > a").hover(function() {
		$(this).find('img:eq(0)').hide();		 
		$(this).find('img:eq(1)').show();		
	}, function() {
		$(this).find('img:eq(1)').hide();	
		$(this).find('img:eq(0)').show();
	});		
	
	// FANCY LIGHTBOX
	$(".fancypage").fancybox({
		'titleShow'		    : false,							 
		'width'				: '75%',
		'height'			: '75%',
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});	
	$(".fancyimg").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'none',
		'transitionOut'	: 'none'
    });
    $(".fancyimg_").fancybox({
        'titleShow': false,
        'transitionIn': 'none',
        'transitionOut': 'none'
    });
	$(".fancytest").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'none',
		'transitionOut'	: 'none'
	});	

	//  HIDE And SELECT RANDOM HOME CTA
	var random=Math.floor(Math.random()*5)
	$("#ctaRandom").find('a:eq('+random+')').show();
	
	// HOME HERO SLIDER
	$(document).everyTime(5000, "movetime",function() { slideDots(); }, 0);  
	
	// new cases nav
	var oldDot = 0;
	var currentDot = 0;
	$('#dot1').css("background-position", "0 -11px");
	$("#hero div img").hide();
	$("#hero div").find('img:eq(0)').show();
	
	function slideDots() {
		oldDot = currentDot;
		currentDot++;
		if(currentDot > 4)
			currentDot = 0;
		findDot();
		moveSlides();		
	}
		
	$('#dot1')
		.bind('click', function(){
		// Determine new position
		oldDot = currentDot;
		currentDot = 0;
		findDot();
		moveSlides();
		$(document).stopTime("movetime");
		$(document).everyTime(5000, "movetime",function() { slideDots(); }, 0); 
	});		
	$('#dot2')
		.bind('click', function(){
		// Determine new position
		oldDot = currentDot;
		currentDot = 1;
		findDot();
		moveSlides();
		$(document).stopTime("movetime");
		$(document).everyTime(5000, "movetime",function() { slideDots(); }, 0); 		
	});	
	$('#dot3')
		.bind('click', function(){
		// Determine new position
		oldDot = currentDot;
		currentDot = 2;
		findDot();
		moveSlides();
		$(document).stopTime("movetime");
		$(document).everyTime(5000, "movetime",function() { slideDots(); }, 0); 		
	});	
	$('#dot4')
		.bind('click', function(){
		// Determine new position
		oldDot = currentDot;
		currentDot = 3;
		findDot();
		moveSlides();
		$(document).stopTime("movetime");
		$(document).everyTime(5000, "movetime",function() { slideDots(); }, 0); 		
	});
	$('#dot5')
		.bind('click', function(){
		// Determine new position
		oldDot = currentDot;
		currentDot = 4;
		findDot();
		moveSlides();
		$(document).stopTime("movetime");
		$(document).everyTime(5000, "movetime",function() { slideDots(); }, 0); 		
	});		
	
	function moveSlides() {
		$("#hero div").find('img:eq('+oldDot+')').fadeOut(1600);
		$("#hero div").find('img:eq('+currentDot+')').fadeIn(1600);
		/*$('#hero div').animate({
			marginLeft : 912*(-currentDot)
		}, 700, 'linear');*/
	}
	
	function findDot()  {
		if(currentDot >= 4) {
			currentDot = 4;
		}
		if(currentDot <= 0) {
			currentDot = 0;
		}	
		$('.dot').css("background-position", "0 0");		
		$('.dot').eq(currentDot).css("background-position", "0 -11px");
	}
	
	// TABS
//	$('.tab').hide().eq(0).show();
//	$('#tav a').click( function() {
//		$('#tabs .tab').hide();
//		$('#tav a').removeClass('active')
//		$('#tabs .tab').eq($('#tav a').index(this)).show();
//		$(this).addClass('active')
//		return false;	
//	}).eq(0).addClass('active');
	
	// WHERE TO BUY BUTTON
	$('#whereBuy').click( function() {
		$('#tabs .tab').hide();
		$('#tav a').removeClass('active')
		$('#tabs .tab').eq(2).show();
		$('#tav a').eq(2).addClass('active')			
	})
	
	// TABLE ALT STYLE
	$('table tr:odd').addClass('alt')
	
	// FAQ
	$('#faq .answer').hide();
	$('#faq .question').toggle(
	function() {
		$(this).addClass('open');
		$(this).parent('li').children('.answer').show()
	},
	function() {
		$(this).removeClass('open');
		$(this).parent('li').children('.answer').hide();
	})
	
	// PRODUCT VIEWER THUMBS
	$('#large li').hide().eq(0).show();
	$('#thumbs li').css({opacity:.8}).eq(0).css({opacity:1}).addClass('active');
	$('#th_left').css({opacity:.2, cursor:'default'})
	$('#thumbs li').click( function() {
		$('#thumbs li').css({opacity:.8}).removeClass('active').eq($('#thumbs li').index(this)).css({opacity:1}).addClass('active');
		$('#large li').hide().eq($('#thumbs li').index(this)).show();
	})
	
	//PRODUCT VIEWER SCROLL
	var th = $('#thumbs li').size();
	if (th<5) {
		$('#th_right').css({opacity:.2, cursor:'default'})
	}
	else {
		var pvc = 4;
		$('#th_right').click( function() {
			if (pvc<th) {
				$('#th_left').css({opacity:1, cursor:''})
				$('#thumbs .window ul').stop(1,1).animate({left:'-=63'})
				++pvc;
				if (pvc==th) {
					$(this).css({opacity:.2, cursor:'default'})					
				}
			}
		})
		$('#th_left').click( function() {
			if (pvc>4) {
				$('#th_right').css({opacity:1, cursor:''})
				$('#thumbs .window ul').stop(1,1).animate({left:'+=63'})
				--pvc;
				if (pvc==4) {
					$(this).css({opacity:.2, cursor:'default'})
				}
			}
		})
	}
	
	// SIDENAV
	$('#sideNav li').each( function() {
		if ($('ul', this).length) {
			$(this).addClass('parent')	;
		}
	});
	$('#sideNav .parent>a').bind('click', 
	function() {
		if ( !$(this).hasClass('open') ) {
			//alert('a')
			$(this).addClass('open')
			$('ul', $(this).parent('li')).show();
		}
		else {
			//alert('b')
			$(this).removeClass('open')
			$('ul', $(this).parent('li')).hide();
		}
		return false;
	})
	
});
	
