jQuery(document).ready( function() {
        jQuery('#tabs').frozTabs({slider: 'froz-tabs-slider', tabLeftPos: 10});
        
        jQuery('a.fancybox-enable').fancybox({
            cyclic: true,
            titlePosition: 'over',
            centerOnScroll: true
        });
        
        jQuery('.back-to-top').click(function(){
            jQuery.scrollTo('#content', 800);
            return false;
            
            
        });
        
        jQuery('.sign-up-input-txt').autofill({value: 'Your E-mail Address'});
        
        jQuery('.section-nav-slider').find('img:last').load( function(){
            jQuery('.section-nav-slider').each( function(){
                
                var headDiv = jQuery(this);
                
                var bodyClass = jQuery('body').attr('class');
                headDiv.data('permaActive', null);

                if ( bodyClass !== '' ){
                    headDiv.data('permaActive', headDiv.find('#section-nav').find('li.' + bodyClass));
                } 
                
                var targets = jQuery(this).find('ul#section-image').children('li');
                headDiv.data('targets', targets)
                       .data('zeroLeft', jQuery(this).find('#section-image').find('li:first').position().left);
    //                   .data('zeroLeft', 0);
                var inTime = 400;
                var outTime = 400;
                var inEasing = 'easeInOutExpo';
                var outEasing = 'easeInOutExpo';
                var distanceMultiplier = 100;
                var activePanelClass = 'active-panel';
                var activeTabClass = 'active-tab';
                var activeZIndex = 10;

                var showPanel = function(count){
                    tempTime = inTime + (count * distanceMultiplier);
                    var target = jQuery(headDiv.data('targets')[count]).addClass(activePanelClass);
                    headDiv.find('#section-nav').find('li').eq(count).addClass(activeTabClass);
                    target.css({zIndex: activeZIndex})
                          .append(target.data('panel').css({zIndex: activeZIndex}).stop(true, true).show());
                    target.stop(true, false).animate({
                        left: headDiv.data('zeroLeft') + 'px',
                        width: target.data('newWidth') + 'px'
                    }, {duration: tempTime, easing: inEasing, queue: false})
                    .find('img:first').stop(true, true).fadeTo(tempTime, 0.01)
                }
                
                var hidePanel = function(count){
                    tempTime = outTime + (count * distanceMultiplier);
                    var target = jQuery(headDiv.data('targets')[count]).removeClass(activePanelClass);
                    headDiv.find('#section-nav').find('li').eq(count).removeClass(activeTabClass);
                    target.css({zIndex: target.data('defIndex')})
                          .data('panel').css({zIndex: target.data('defIndex')}).fadeOut(tempTime);
                    target.stop(true, false).animate({
                        left: target.data('position').left + 'px',
                        width: target.data('origWidth')
                    }, {duration: tempTime, easing: outEasing, queue: false})
                    .find('img:first').stop(true, false).fadeTo((tempTime * 1.8), 1)
                }
                
                var hideAll = function(count){
                    headDiv.data('targets').each( function(){
                        if ( jQuery(this).hasClass(activePanelClass) ){
                            var index = jQuery(this).index();
                            if ( index != count ){
                                jQuery(this).data('hide')(index);
                            }
                        }
                    })
                }
                
                headDiv.data('targets').each( function(){
                    var count = jQuery(this).index();
                    var panel = headDiv.find('#section-panel')
                                       .children('li:eq(' + count + ')')
                                       .css({position: 'absolute', top:'0px', left: '0px', zIndex: activeZIndex})
                                       .children('div:first')
                                       .css({position: 'absolute', top:'0px', left: '0px', zIndex: activeZIndex});
                    jQuery(this).data('panel', panel)
                                .data('show', showPanel)
                                .data('hide', hidePanel)
                                .data('hideAll', hideAll)
                                .data('origWidth', jQuery(this).css('width'))
                                .data('newWidth', jQuery(this).data('panel').outerWidth(true))
                                .data('position', jQuery(this).position())
                                .data('defIndex', jQuery(this).css('z-index'))
    //                            .find('img:first').css({position:'relative'})
                }).each( function(){
                    jQuery(this).css({overflow: 'hidden', 
                                      position: 'absolute',
                                      left: jQuery(this).data('position').left + 'px',
                                      top: jQuery(this).data('position').top + 'px'
                                    })
                }).data('panel').mouseover( function(e){
                    e.stopPropagation();
                }).mouseleave( function(e){
    //                e.stopPropagation();
                })
                
                jQuery(this).mouseleave( function(){
                    hideAll(-1);
                    if ( jQuery(this).data('permaActive') !== null ){
                        jQuery(this).data('permaActive').trigger('mouseover');
                    }
                }).find('ul#section-image > li > img').mouseover( function(){
                    var count = jQuery(this).parent('li').index();
                    jQuery(headDiv.data('targets')[count]).siblings().data('hideAll')(count)
                    jQuery(headDiv.data('targets')[count]).data('show')(count);
                })
                
                jQuery(this).find('ul#section-nav > li').mouseover( function(){
                    var count = jQuery(this).index();
                    jQuery(headDiv.data('targets')[count]).find('img:first').trigger('mouseover');
                })
                
                
                if ( jQuery(this).data('permaActive') !== null ){
                    var temp = inTime;
                    inTime = 0;
                    jQuery(this).data('permaActive').trigger('mouseover');
                    inTime = temp;
                }
                
            })
        })
});

// $j = jQuery.noConflict();

///Original Code Created Posted In The Ecwid Forum By Eugene

//Adapted by Andrew Leonard of EcwidMadeEasy.com to show the terms and conditions checkbox in the SHopping Cart

// On line 48 below YOU MUST REPLACE ************** with the url that contains your terms and conditions.


// Bind a change handler to the window location.
jQuery( window.location ).bind("change", function(objEvent, objData){
    var page = {};
    hash = objData.currentHash.slice(6);  //trim the ecwid and the COLON leading.
    page.mode = getPageType(hash);
    page.category = getCategory(hash);
    page.product = getProduct(hash);
    runMods(page);  //If the window's changed, run the mods.
});

function runMods(page) {
    
    // console.log('------ Run Mods ------');
    // console.log(page);
    
       //Call anything you want on every page here.
    functionComplete = false;  //They all retrun true if successful. false if not.
    if (window.t1 !== undefined) { 
        clearInterval(t1); 
    }
    
    t1 = setInterval (function() {
        switch(page.mode) {
            case 'cart':
                    functionComplete = processModCart();
                break;
            case 'checkoutSA':
            case 'checkoutPD':
                    functionComplete = processModCheckOut();
                break;
//          case "checkoutPO":                  //Original code for it to work after the checkout button is selected (eg if phone ordering has been selected)                   
            // case "cart":                        // Change made by EcwidMadeEasy.com for it to work in Shopping Cart - decides which page it is displayed on
            //         functionComplete = process_placeorder_button();
            //     break;
            default:
                //Nothing to do!
                functionComplete = true;
                break;
        }
          
          if (functionComplete) { 
              clearInterval(t1); 
          }
    }, 100);
}

function getCityList()
{
    return '<select id="city-list" class="gwt-ListBox">' + 
                '<option value="9">Agana</option>' + 
                '<option value="17">Agat</option>' + 
                '<option value="9">Anigua</option>' + 
                '<option value="17">Apra Hts</option>' + 
                '<option value="10">Asan</option>' + 
                '<option value="7">Back Road (Anderson)</option>' + 
                '<option value="8">Barrigada</option>' + 
                '<option value="9">Chalan Pago</option>' + 
                '<option value="6">Dededo</option>' + 
                '<option value="4">Harmon</option>' + 
                '<option value="20">Inarajan</option>' + 
                '<option value="9">Maina</option>' + 
                '<option value="9">Maite</option>' + 
                '<option value="4">Mall Area</option>' + 
                '<option value="9">Mangilao</option>' + 
                '<option value="20">Merizo</option>' + 
                '<option value="8">Mongmong</option>' + 
                '<option value="17">Naval Station</option>' + 
                '<option value="7">NCS</option>' + 
                '<option value="8">Pagat (Mangilao)</option>' + 
                '<option value="11">Piti</option>' + 
                '<option value="17">Santa Rita</option>' + 
                '<option value="9">Sinajana</option>' + 
                '<option value="20">Talofofo</option>' + 
                '<option value="7">Tamuning</option>' + 
                '<option value="7">Tiyan</option>' + 
                '<option value="8">Toto</option>' + 
                '<option value="6">Tumon</option>' + 
                '<option value="20">Umatac</option>' + 
                '<option value="6">Upper Tumon</option>' + 
                '<option value="11">USO</option>' + 
                '<option value="8">Yigo</option>' + 
                '<option value="10">Yona</option>' +
    '</select>';
}

function processModCart()
{
    console.log('Processing Mod Cart');
    
    
    var changeShippingButton = jQuery('.ecwid-productBrowser-cart-rightPanel .ecwid-productBrowser-cart-estimation button.gwt-Button');
    var shippingNote         = jQuery('.ecwid-productBrowser-cart-rightPanel .ecwid-productBrowser-cart-estimationNote');
    
    if (changeShippingButton.length) {
        console.log(changeShippingButton);
        
        if (shippingNote.length) {
            var shippingNoteHtml = shippingNote.html();
            shippingNoteHtml = shippingNoteHtml.replace(/([\d]+,)+/, '');
            shippingNote.html(shippingNoteHtml);
        }
        
        changeShippingButton.click(function(){
            
            console.log('Change Shipping Clicked');
            
            var countryFieldEl = null,
                countryLabelEl = null,
                // cityFieldEl    = null,
                // cityLabelEl    = null,
                zipFieldEl     = null,
                zipLabelEl     = null,
                stateFieldEl   = null,
                stateLabelEl   = null,
                updateButtonEl = null;
            
            var els = jQuery( '.ecwid-form .ecwid-fieldLabel' );
            
            
            
            
            
            if (els.length) {
                
                    
                    if (jQuery('button.gwt-Button.ecwid-productBrowser-cart-chooseLocationPopup-getQuoteButton').length){
                        updateButtonEl = jQuery('button.gwt-Button.ecwid-productBrowser-cart-chooseLocationPopup-getQuoteButton');
                    }
                    
                    
                    
                    els.each(function(i){
                        
                        // If Country is Guam,
                        // Then the City label should be replaced with (City/Villages) dropdown
                        if ('Country' === jQuery(this).text()) {
                            countryLabelEl = jQuery(this);
                            countryFieldEl = jQuery('#' + countryLabelEl.attr('for') + ' .gwt-ListBox');
                        }

                        // if ('City' === jQuery(this).text() || 'Village / Area' === jQuery(this).text()) {
                        //     cityLabelEl = jQuery(this);
                        //     cityFieldEl = jQuery('#' + cityLabelEl.attr('for'));
                        // }
                        
                        if ('State' === jQuery(this).text() || 'Village / Area' === jQuery(this).text()) {
                            stateLabelEl = jQuery(this);
                            stateFieldEl = jQuery('#' + stateLabelEl.attr('for') + ' .gwt-TextBox');
                        }

                        if ('Postal Code' === jQuery(this).text()) {
                            zipLabelEl = jQuery(this);
                            zipFieldEl = jQuery('#' + zipLabelEl.attr('for') + ' .gwt-TextBox');
                        }
                        
                        


                    });

                    // console.log(cityLabelEl);
                    // console.log(cityFieldEl);
                    console.log(countryLabelEl);
                    console.log(countryFieldEl);
                    console.log(stateLabelEl);
                    console.log(stateFieldEl);
                    console.log(zipLabelEl);
                    console.log(zipFieldEl);
                    console.log(updateButtonEl);
                    
                    
                    var updateCityField = function()
                    {
                        // If Country is Guam
                        if ('GU' === countryFieldEl.val()) {
                        
                            // Treat state as a village
                            stateLabelEl.text('Village / Area');
                        
                            if (jQuery('#city-list').length) {
                                var cityList = jQuery('#city-list');
                            } else {
                                var cityListStr = getCityList();
                                var cityList    = jQuery(cityListStr);
                                stateFieldEl.after(cityList);
                            }
                        
                            var updateZipValue = function(newZipValue)
                            {
                                // cityFieldEl.val(cityList.find('option:selected').text());
                                zipFieldEl.val(newZipValue);
                                updateButtonEl.click();
                                stateFieldEl.hide();
                                zipLabelEl.hide();
                                zipFieldEl.hide();
                                cityList.show();
                            };

                            jQuery(cityList).change(function(){
                                console.log('City List Changed');
                                updateZipValue(jQuery(this).val());
                            });

                            updateZipValue(cityList.val());
                        
                        } else {
                            stateLabelEl.text('State');
                            stateFieldEl.show();
                            zipFieldEl.val('');
                            // cityFieldEl.val('');
                            // cityFieldEl.show();
                            zipLabelEl.show();
                            zipFieldEl.show();
                            
                            updateButtonEl.click();
                            
                            if (jQuery('#city-list').length) {
                                jQuery('#city-list').hide();
                            }
                        }
                    };
                    
                    updateCityField();
                    
                    
                    countryFieldEl.change(function(){
                        updateCityField();
                        
                        console.log('Country Field Value Changed');
                    });
                    
                    
                
            }
            
        });
        
        // var els = jQuery('.ecwid-popup-content .ecwid-form .ecwid-fieldLabel');
        // console.log(els);
        
        return true;
    }
    
    return false;
    
    
}



function processModCheckOut()
{
    var els = jQuery( ".ecwid-fieldWrapper .ecwid-fieldLabel" );
    
    if (els.length) {
        // console.log(els);
        
        var countryFieldEl = null,
            countryLabelEl = null,
            cityFieldEl    = null,
            cityLabelEl    = null,
            zipFieldEl     = null,
            zipLabelEl     = null,
            stateLabelEl   = null
            stateFieldEl   = null;
        
        els.each(function(i){
            
            
            // If Country is Guam,
            // Then the City label should be replaced with (City/Villages) dropdown
            
            if ('Country' === jQuery(this).text()) {
                countryLabelEl = jQuery(this);
                countryFieldEl = jQuery('#' + countryLabelEl.attr('for'));
            }
            
            if ('City' === jQuery(this).text() || 'Village / Area' === jQuery(this).text()) {
                cityLabelEl = jQuery(this);
                cityFieldEl = jQuery('#' + cityLabelEl.attr('for'));
            }
            
            if ('Zip/Postal Code' === jQuery(this).text()) {
                zipLabelEl = jQuery(this);
                zipFieldEl = jQuery('#' + zipLabelEl.attr('for'));
            }
            
            if ('State/Province' === jQuery(this).text()) {
                stateLabelEl = jQuery(this);
                stateFieldEl = jQuery('#' + stateLabelEl.attr('for'));
            }
            
        });
        
        console.log(cityLabelEl);
        console.log(cityFieldEl);
        console.log(countryLabelEl);
        console.log(countryFieldEl);
        console.log(zipLabelEl);
        console.log(zipFieldEl);
        console.log(stateLabelEl);
        console.log(stateFieldEl);
        
        
        
        
        var updateCityField = function()
        {
            
            
            // If Country is Guam
            if ('GU' === countryFieldEl.val()) {
                cityLabelEl.text('Village / Area');
                
                
                if (jQuery('#city-list').length) {
                    var cityList = jQuery('#city-list');
                } else {
                    var cityListStr = getCityList();
                    var cityList = jQuery(cityListStr);
                    cityFieldEl.after(cityList);
                }
                
                
                
                var updateZipValue = function(newZipValue)
                {
                    
                    cityFieldEl.val(cityList.find('option:selected').text());
                    zipFieldEl.val(newZipValue);
                    zipFieldEl.change();
                    cityFieldEl.hide();
                    
                    zipFieldEl.closest('.ecwid-fieldWrapper').hide();
                    stateFieldEl.closest('.ecwid-fieldWrapper').hide();
                    // zipFieldEl.hide();
                    // zipLabelEl.hide();
                    // stateLabelEl.hide();
                    // stateFieldEl.hide();
                    
                    cityList.show();
                    
                    
                };
                
                jQuery(cityList).change(function(){
                    console.log('City List Changed');
                    updateZipValue(jQuery(this).val());
                });
                
                updateZipValue(cityList.val());
            } else {
                cityLabelEl.text('City');
                zipFieldEl.val('');
                cityFieldEl.val('');
                cityFieldEl.show();
                //                 zipFieldEl.show();
                
                zipFieldEl.closest('.ecwid-fieldWrapper').show();
                stateFieldEl.closest('.ecwid-fieldWrapper').show();
                
                if (jQuery('#city-list').length) {
                    jQuery('#city-list').hide();
                }
            }
            
            
            
        };
        
        updateCityField();
        
        countryFieldEl.change(function(){
            updateCityField();
            console.log('Country Field Value Changed');
        });
        
        
        return true;
        
    } else {
        return false;
    }
    
    return false;
    
}


jQuery( document ).ready(function() {
           t2 = setInterval (function() {
               // processModCheckOut();
               // process_placeorder_button();
      }, 50);
});



