﻿jQuery.noConflict();

var AjaxCount = 0;

jQuery(function() {

FixEverestBoxes();
//$('<div class="LoadP" Loading Promotions...</div>').insertBefore(jQuery('#ContentCycle'));
jQuery('#ContentCycle').hide();

jQuery(".FO_AJAX").each(function() {
        // AJAX container should be an a link with the href set to the target page content
AjaxCount++;
        
        var href = jQuery(this).attr("href");
        var thisA = jQuery(this);
        var newElem  = jQuery("<div class=\"FO_AJAX_LOADING\">Loading external content.</div>");
        thisA.replaceWith(newElem);
        thisA = newElem;

        jQuery.get(href, function(data) {
           
            if (data == "") {
                thisA.replaceWith("Not Loaded.");
                return;
            }
            
            thisA.replaceWith(data);

          // Once everything is downloaded, start the cycle
          // May not start if there is an error. WIll need to test.
          IsAjaxDone(data);
     

        });


    });
    
    jQuery("#h_banner").insertBefore(jQuery("#mc_middle .ItemCatalogue")).show();
    jQuery(".floatright img[src='App_Themes/EVEREST/Images/button_callforpricev4.png']").width(172);
    
    // Catalog Rotators
    
    setInterval("RecursiveFade()", 8000);

});

function FixEverestBoxes()
{

  jQuery(".navBox, .navBox_secondary, .navBox_related").each(function() {
    jQuery(this).addClass("ui-widget ui-widget-content ui-corner-all");
    jQuery('.nb_list',this).prependTo(jQuery(this));
      jQuery('h3',this).prependTo(jQuery(this)).addClass('ui-widget-header ui-corner-top');
      jQuery('.nb_heading, .nb_footer', jQuery(this)).remove();
    
  });
}

function RecursiveFade()
{
  jQuery(".CatalogRotate .SideContainer").each(function() { 


    jQuery(".Product:eq(0)", jQuery(this)).fadeOut(1000, function(){

      jQuery(this).parent().prepend(jQuery('<div style="width: 130px; height: 180px; float: left;"></div>').animate({width: 0}, 500, function(){jQuery(this).remove();}));
      

 
      jQuery(this).insertAfter(jQuery(".Product:last",jQuery(this).parent()));
      
      jQuery(this).fadeIn(1000);
     

    });



  });
     
  
}

function IsAjaxDone(data) {
AjaxCount--;


if (AjaxCount == 0)
{
 jQuery('#ContentCycle').show();
 //jQuery('.LoadP').hide();
 
 // Taken from the youtube code
 //if (LoadVideoBar)
  //  LoadVideoBar();
  //if (GSearch)
  //  GSearch.setOnLoadCallback(LoadVideoBar);
 

  if (jQuery('#ContentCycle').length != 0)
    {
    jQuery('#ContentCycle') 
    .before('<div id="cnav">') 
    .cycle({ 
        fx:     'scrollLeft', 
        speed:  'fast', 
        timeout: 10000, 
        pager:  '#cnav',
        fit: 1
        
    });

    // Add Play/Pause Button
    jQuery("#cnav").append(jQuery('<a href="#" style="float: right;margin-right:2px;"><span class="ui-icon ui-icon-pause"></span></a>').click(function() {
      if (jQuery('.ui-icon-pause',this).length != 0)
      {  
        jQuery('#ContentCycle').cycle('pause');
        jQuery('.ui-icon-pause',this).toggleClass('ui-icon-pause ui-icon-play');
      }
      else
      {
        jQuery('#ContentCycle').cycle('resume');
        jQuery('.ui-icon-play',this).toggleClass('ui-icon-pause ui-icon-play');
      }
      
      

      }));
      jQuery("#cnav a").button();
      jQuery("#cnav").addClass("ui-widget-header ui-corner-top").buttonset();
      
     

    }
  }
}
