/* global navigation */

$(function() {
	$('div#region-menu').jScrollPane({
		showArrows: true
	});
	$('div.dropdown').hide();
	$('div.dropdown').siblings().mouseover(showMenu);
	$('div.dropdown').siblings().mouseout(leaveMenu);
	$('div.dropdown').mouseover(showMenu);
	$('div.dropdown').mouseout(leaveMenu);	

	/*jQuery Tab Initiation*/
	$("#tabbed-content-holder").tabs();
	$("#tabbed-product-holder").tabs();

	/*jQuery Fancybox Initiation*/
	$("a.single_image").fancybox({
	    overlayColor: '#000',
	    titlePosition: 'inside'
	});


});

var closeTimer;

function showMenu(){
	clearTimeout(closeTimer);
	var target = $(this).hasClass('dropdown') ? $(this) : $(this).siblings('div.dropdown');
	hideAllMenus();
	target.show();
	target.siblings('a').addClass('nav-on');
}

function leaveMenu(menu){
    var target = $(this).hasClass('dropdown') ? $(this) : $(this).siblings('div.dropdown');
    if ($(this).hasClass('regiondropdown'))
        closeTimer = setTimeout('hideAllMenus()', 200);
    else
	    closeTimer=setTimeout('hideAllMenus()',750);
}


function hideAllMenus(){
	var menus = $('div.dropdown');
	menus.hide();
	menus.siblings('a').removeClass('nav-on');
}


$(function() {

	/* tip-downs */
	
	$('div.tipdowns').tipdown({
		trigger: 'div.tip-header',
		content: 'div',
		openClass: 'tip-header-open'
	});
	
	/* featured product slide shows */
	
	$('div.product-slideshow').slideshow({
			animationStyle: 'crossfade',
			autoPlay: false,
			nextButton: 'a.product-slideshow-next',
			prevButton: 'a.product-slideshow-back'
	});
	
	/* product detail selector */
		
//	$('div.product-detail').hide();
//	$('div#product-style-selector select').change(function(){
//		$('div#product-styles-overview, div.product-detail').hide();
//		($(this).val() == 'overview') ? $('div#product-styles-overview').show() : $('div.product-detail').eq($(this).val()).show();
//	});
//	
//	$('div.product-style').each(function(){
//		var index = $(this).index();
//		$(this).find('a').attr('href', '#').click(function(){
//			$('div#product-styles-overview').hide();
//			$('div.product-detail').eq(index).show();
//			$('div#product-style-selector select').val(index);
//			return false;
//		});
//	});
//	
	/* product detail gallery */
	
	$('div.product-detail-gallery').slideshow({
			animationStyle: 'crossfade',
			autoPlay: false,
			useNav: 'div.product-detail-gallery-nav a'
	});
	
	/* homepage interactive */

	$('div#layout-home-top').slideshow({
	    autoPlay: 10000,
	    slides: 'div.home-slide',
	    useNav: '#home-interactive-nav a',
	    slideExit: function() {
	        $('div.home-overlay').animate({
	            // fade out overlay boxes, then place back in starting position
	            opacity: 0
	        }, 'fast', function() {
	            $('div.home-overlay').css({
	                opacity: 0,
	                left: '500px',
	                top: '75px'
	            });
	        });
	    },
	    slideEnter: function() {
	        $('div.home-overlay').animate({
	            // animate overlay boxes in from right
	            opacity: 1,
	            left: '425px',
	            top: '75px'
	        });
	    }
	});

	$('a.video-link').fancybox({
	    padding: '0',
	    margin: '0',
	    transitionIn: 'none',
	    transitionOut: 'fade',
	    type: 'iframe',
	    width: 854,
	    height: 512, // height + 32px for control bar
	    autoDimensions: true,
	    autoScale: false,
	    scrolling: 'no',
	    onStart: function() { $('div#layout-home-top').slideshow('stopAutoPlay') }
	});

	$('a.sidebar-video').fancybox({
	    padding: '0',
	    margin: '0',
	    transitionIn: 'none',
	    transitionOut: 'fade',
	    type: 'iframe',
	    width: 640,
	    height: 392, // height + 32px for control bar
	    autoDimensions: true,
	    autoScale: false,
	    scrolling: 'no'
	});
	
	/* homepage curation station */

	$('div#layout-home-bottom-col3').slideshow({
			slides: 'div.social-slide',
			autoPlay: false,
			nextButton: 'a#next-social-message'
	});

});

/* find a pro search */
function clearTextFieldDefault(txtFieldID, defaultValue) {
    var txt = document.getElementById(txtFieldID);
    if (txt.value == defaultValue)
        txt.value = "";
}

function restoreTextFieldDefault(txtFieldID, defaultValue) {
    var txt = document.getElementById(txtFieldID);
    if (txt.value == "")
        txt.value = defaultValue;
}

function submitFindProSearchOnEnter(e, txtFieldID) {
    var CodeForEnter = 13;
    var codeEnteredByUser;

    if (!e) var e = window.event;
    if (e.keyCode) codeEnteredByUser = e.keyCode;
    else if (e.which) codeEnteredByUser = e.which;

    var txt = document.getElementById(txtFieldID);

    if (codeEnteredByUser == CodeForEnter) {
        window.location.href="/find-a-professional?zipcode=" + txt.value;
    }
}


