/* Scroll Pane
--------------------------------------------------------------------------------------- */
jQuery(function($)
{
    if (showTabId) {
        ShowTab(showTabId);
    }
    else InitTabs();
    $('#textArea').fadeIn('slow');
    $('.jScrollPane').jScrollPane({scrollbarWidth:16, scrollbarMargin:0,showArrows:true});
});

var tabs_initiated=false;

function InitTabs(options){
    /*** this function requires dimension plugin ***/
    if(tabs_initiated) return true;
    tabs_initiated=true;
    
    var defaults={simulate: false,coor: {x:0,y:0}};
    var settings=$.extend(defaults,options);
    
    var current_tab,hover_tab,active_tab;

    var GetHoverTab=function(coor){
        if (-430 <= coor.y &&  coor.y<= -400){
            if (-10  >= coor.x && coor.x > -113) return 'wine1Wrapper';
            else if(-113 >= coor.x && coor.x > -215) return 'wine2Wrapper';
            else if(-215 >= coor.x && coor.x > -350) return 'wine3Wrapper';
        }
        return '';
    }

    var HoverTab=function(options){
        var settings=options;
        $('#menuWrapper > div').each(
            function(){
                if ($(this).attr('id') != active_tab)
                    $(this).css({'z-index':9}).unbind('click');
            });
        current_tab=hover_tab;
        hover_tab='';

        $('#'+current_tab).css({'z-index':10}).bind('click',function(){return AnimateTab(settings);});

        if (settings.simulate == true) {
            $('#'+current_tab).click();
        }

    };

    var AnimateTab=function(options){
        var setting=options;
        if(active_tab !== ''){
            $('#'+active_tab)
            .css({'z-index':8})
            .fadeOut('slow',function(){
                $('div:gt(0)',this).hide()
                .parent().hide()
                .css({'z-index':9})
                .slideDown();
            });
        }
        if (settings.simulate){
            $('#'+current_tab).css({'z-index':7}).find('div:gt(0)').show(function(){return onTabActivated(this);});
        }else{
            $('#'+current_tab).slideUp(function(){
                $(this)
                .css({'z-index':7})
                .slideDown('fast',function(){
                    $('div:gt(0)',this)
                    .slideDown(function(){
                        return onTabActivated(this);
                    });
                });
            });
        }
        active_tab=current_tab;
        current_tab='';
    };

    if (settings.simulate){
        hover_tab = GetHoverTab(settings.coor);
        HoverTab(settings);
        $('#menuWrapper > div').filter(function(){
            return $(this).attr('id')==active_tab?false:true;
        }).each(function(){$('div:gt(0)', this).hide();});
    }else{
        $('#menuWrapper > div').each(function(){$('div:gt(0)', this).hide();});
    }
    
    // find the mouse coordinates and hover the tab accordingly.
    $('#menuWrapper').unbind('mousemove').mousemove(function(e){
        var mouse_coor={};
        mouse_coor.x = $('#menuWrapper').offsetParent().position().left - e.pageX;
        mouse_coor.y = $('#menuWrapper').offsetParent().position().top - e.pageY;

        if (-430 <= mouse_coor.y &&  mouse_coor.y<= -400){
            hover_tab=GetHoverTab(mouse_coor);
            if (hover_tab != current_tab && hover_tab != active_tab) HoverTab(settings);
        }
    });
    
    settings.simulate = false;
}

function ShowTab(id){
    var options = {};
    options.simulate=true;
    options.coor={};
    options.coor.y=-425;
    switch(id){
        // id #4: Red Wine
        // id #5: White Wine
        // id #6: Dessert Wine
        case 4: options.coor.x = -100; break;
        case 5: options.coor.x = -200; break;
        case 6: options.coor.x = -300; break;
    }
    
    return InitTabs(options);
}

function onTabActivated(tab){
    $('a',tab).each(function(){
        $(this).unbind('click').bind('click',function(){
            var url=$(this).attr('href');
            $('#textArea').fadeOut('fast',function(){window.location=url;});
            return false;
        });
    });
}

/* LightBox Script
--------------------------------------------------------------------------------------- */
$(function() {
  $('a[rel*="gallery"]').lightBox({
  	overlayBgColor: '#000000',
	overlayOpacity: 0.8,
	imageLoading: 'https://www.deserthills.ca/images/lightbox/lightbox-ico-loading.gif',
	imageBtnClose: 'https://www.deserthills.ca/images/lightbox/lightbox-btn-close.gif',
	imageBtnPrev: 'https://www.deserthills.ca/images/lightbox/lightbox-btn-prev.gif',
	imageBtnNext: 'https://www.deserthills.ca/images/lightbox/lightbox-btn-next.gif',
	imageBlank: 'https://www.deserthills.ca/images/lightbox/lightbox-blank.gif',
	containerResizeSpeed: 400,
	txtImage: 'Photo',
	txtOf: 'of',
	keyToClose: 'c',
	keyToPrev: 'p',
	keyToNext: 'n'
   });
});

/* DETECT NAVIGATION ON STATE SCRIPT
--------------------------------------------------------------------------------------- */
$(document).ready(function(){
	var url = location.pathname.substring(1);
	var path = url.split("/");
	path.shift();
	if(path.length>1){
		path.pop();
		path='/'+path.join('/')+'/';
	}
	else path=path[0];
	
	if ( path ) {
		$('#nav li a[href*="' + path + '"]').addClass('selected');
	} else if (url) {
		$('#nav li a[href*="' + url + '"]').addClass('selected');
	} else {
		$('.home a').addClass('selected');
	}
	
});
