jQuery(document).ready(function($) {
    
    jQuery(window).bind( 'hashchange', updateOnHashChange);
    $(window).trigger( 'hashchange' );
    
    $('form input.submit').hover(
        function(){
            $(this).addClass('hovered')
        },
        function(){
            $(this).removeClass('hovered');
    });
    
    $('.highlight-cart').mouseover(function() {
        $('form input.submit').css({ 'borderColor': '#F5F74A'})
            .animate(
                { 
                    borderTopColor: "#ffffff", borderRightColor: "#ffffff", 
                    borderBottomColor: "#ffffff", borderLeftColor: "#ffffff"
                }, 2000);
    });
    
    $('table.product-descriptions tbody tr:even').addClass('odd');
    
    $('#product-descriptions tr').hoverIntent(
        function(){
            rel = $(this).attr('rel');
            img = '#img-' + rel;
    
            if(rel == undefined || $(img).hasClass('active') ) {
                return;
            }

            index = parseInt($(this).attr('index'));
            if( index != undefined) {
                $('#images').cycle(index);
            }
            
            $('#product-descriptions tr').removeClass('active');
            $(this).addClass('active');
        },
        function(){
            return;
        }
    );
    
    $('#product-descriptions tr, #images div').click(function() {
        rel = $(this).attr('rel');
        if( rel == undefined ) {
            return;
        }
        
        inlineToShow = '#txt-'+rel;
        $.fn.colorbox({ 
            width:    "750px", 
            inline:   true, 
            href:     inlineToShow
        });
        return false;
    });
    
    $('#images').cycle({
        fx: "fade",
        timeout: 8000,
        before:  beforeSlideTransition
    });
    
    $(document).bind('cbox_open', function() {
        $('#images').cycle('pause');
    });
    
    $(document).bind('cbox_closed', function() {
        $('#images').cycle('resume');
        $('#images').cycle('next');
    });
    
    
    $('.colorbox-link').click(function() {
        rel = $(this).attr('rel');
        if( rel == undefined ) {
            return;
        }
        
        inlineToShow = '#txt-'+rel;
        $.fn.colorbox({ 
            width:    "750px", 
            inline:   true, 
            href:     inlineToShow
        });
        return false;
    });
    
});

function beforeSlideTransition(current, next, options, forwardFlag)
{
    $('#product-descriptions tr').removeClass('active');
    rel = $(next).attr('rel');
    row = '#row-'+rel;
    
    $(row).addClass('active');
}
    
function updateOnHashChange(e)
{
    
    var url         = jQuery.param.fragment();   
    var hashArray   = url.split('/');
    var stateArray  = hashArray[0].split(':');
    
    var oldState  = jQuery("input[name='state']").val();
    var oldCounty = jQuery("input[name='county']").val();
    
    stateId     = parseInt(stateArray[0]);
    stateName   = decodeURI(stateArray[1]) || '';
    countyName  = decodeURI(hashArray[1])  || '';

    if( ! isNaN(stateId) )         { flashvars.show_map = stateId;}
    if( stateName == 'undefined')  { stateName  = ''; }
    if( countyName == 'undefined') { countyName = ''; }
    
    if( stateName == '') {
        //$('#state-instructions').fadeOut();
    } else {
        //$('#state-instructions').fadeIn();
    }

    jQuery("input[name='state']").val(stateName);
    jQuery("input[name='county']").val(countyName);
    
    if( oldState != stateName ) {
        jQuery("input[name='state']").css('backgroundColor', '#F5F74A').animate({ backgroundColor: "#ffffff" }, 700);
    }

    if( oldCounty != countyName ) {
        jQuery("input[name='county']").css('backgroundColor', '#F5F74A').animate({ backgroundColor: "#ffffff" }, 700);
    }
}

    var flashvars = {
        "xmlfile1" : "http://www.myolivetree.com/destiny?action=usa",
        "show_map" : 0
    };
    
    var params = {
        "allowScriptAccess" : "always",
        "quality" : "high",
        "bgcolor" : "#ffffff",
        "wmode"   : "transparent"
    };
    //swfobject.embedSWF('site_addons/destinytree/assets/flash/usa.swf', 'flashMap', '546', '400', '6.0.0', 'site_addons/destinytree/assets/flash/expressInstall.swf', flashvars, params);
    swfobject.embedSWF('site_addons/destinytree/assets/flash/usa.swf', 'flashMap', '900', '656', '6.0.0', 'site_addons/destinytree/assets/flash/expressInstall.swf', flashvars, params);
