//
// JQuery Site scripts
//

jQuery.noConflict();
jQuery(document).ready(function(){

    //----------------------------------    
    // Decrypt emails
    //----------------------------------
    jQuery("a[href^='mailto']").each(function(){
      jQuery(this).text(jQuery(this).text().replace(/^(mailto:)(.+):([^\?]+)(\?.+)?$/i,"$3@$2$4"));    
      jQuery(this).attr('href',jQuery(this).attr('href').replace(/^(mailto:)(.+):([^\?]+)(\?.+)?$/i,"$1$3@$2$4"));      
    });
    
    //----------------------------------    
    // Fix backgrounds on image links
    //----------------------------------
    jQuery("a:has(img)").css("background-color","transparent");

    //----------------------------------    
    // Blog/IE7 linked image fix
    //----------------------------------
    if(navigator.userAgent.indexOf("MSIE")>-1) {
      jQuery(".postentry a img").each(function(){
        var a=jQuery(this).attr("align");
        if (a!='') jQuery(this).css("float",a);
      });
    }

    //----------------------------------    
    // Set up tabs
    //----------------------------------
    multitabs(".multitabs");
    
    //----------------------------------    
    // Add more button to blog tabs 
    //----------------------------------     
    jQuery(".blog-tabs").each(function(){
    
      // Disable topics if empty
      if(slides.eq(0).find("ul").length==0){
        tabs.find("a:eq(0)").click(function(){
          return false;}).css("color","#999999");      
        tabs.find("a:eq(1)").click();      
      }

      // Set up more button
      morebox=jQuery(this).find(".more");
      mb=new morebutton(morebox,morebox.parent());
      mb.update(slides.filter(".active"));
         
      // Override multi-tab click event
      // so that more button gets updated
      // when tab changes
      tab_click_slide=function(hs,vs){
        hs.removeClass("active");
        hs.hide();
        mb.update(vs);
        vs.show();
        vs.addClass("active"); 
      }
    });

    //----------------------------------    
    // Customize topics list
    //----------------------------------   
    jQuery(".cards-block-1 .multitabs").each(function(){

      // Override events
      tab_click_slide=function(hs,vs){
        if (hs.length>0) {
          hs.removeClass("active");
          if(hs.is(".slide.topics"))hs.find(".frame").fadeOut("fast");
          hs.slideUp("slow",tab_click_slide_show);
        } else {
          hs.slideUp("slow",tab_click_slide_show);
        }
 
        // Internal function to complete load
        function tab_click_slide_show(){
          if(vs.is(".slide.topics"))vs.find(".frame").fadeIn("fast");
          vs.slideDown("slow",topicsmb.toggle);
          vs.addClass("active");
        }
      }
     
      // Add more button
      var topics=jQuery(".slide.topics");
      var topicsmb=new morebutton(topics.find(".more"),topics.find(".frame"));       
      topicsmb.update(topics.find(".inner-slide"));
      
    });

    //----------------------------------    
    // Add hovers to cards
    //----------------------------------      
    jQuery("#card-gallery .card-thumbnail").each(function(){
      var th=jQuery(this);
      var tko=null;

      jQuery(this).hover(
        function(){
          tko=window.setTimeout(function(){
            var prv=th.find(".preview");
            jQuery("#card-gallery .card-thumbnail").css("z-index",5);
            th.css("z-index",10);
            prv.find("img").attr("src","ImageHandler.aspx?tmplthumblg="+th.attr("id"));
            prv.animate({
              top: "-237px", left: "-167px", width: "575px", height: "415px", opacity: "show"
            },"slow");
          },500)
        },
        function(){
          clearTimeout(tko);
          jQuery(this).find(".preview").animate({
            top: "0", left: "0", width: "100px", height: "100px", opacity: "hide"
          },"slow");
        }
      )
    });

    //----------------------------------    
    // Add hovers to card customize
    //----------------------------------      
    jQuery(".customizecard").each(function(){
      var th=jQuery(this);
      var chk=th.find("#ctl00_cphPage_cbTerms");
      var rad=th.find("#ctl00_cphPage_rbCreateAndPrint");
      var wrn=th.find("#spamwarning");
      var tko=null;

      // Show window when clicked
      chk.click(function(){
        if(rad.is(":checked")) wrn.animate({
          top: "-100px", left: "-100px", width: "350px", height: "200px", opacity: "show"
        },"slow");
      });

      // Set rolloff effect
      wrn.hover(function(){
        clearTimeout(tko);
      },function(){
        tko=window.setTimeout(function(){
          wrn.animate({
            top: "0", left: "0", width: "0", height: "0", opacity: "hide"
          },"slow");
        },500);
      });

    });

    //----------------------------------    
    // Add hovers to marketplace
    //----------------------------------      
    jQuery(".market-listings .listing-thumbnail").each(function(){
      var th=jQuery(this);
      var tko=null;

      jQuery(this).hover(
        function(){
          tko=window.setTimeout(function(){
            var prv=th.find(".preview");
            jQuery(".market-listings .listing-thumbnail").css("z-index",5);
            th.css("z-index",10);
            prv.animate({
              top: "-50px", left: "-50px", width: "400px", height: "250px", opacity: "show"
            },"slow");
          },500)
        },
        function(){
          clearTimeout(tko);
          jQuery(this).find(".preview").animate({
            top: "0", left: "0", width: "100px", height: "100px", opacity: "hide"
          },"slow");
        }
      )
    });

    //----------------------------------    
    // Add hovers to product photo gallery
    //----------------------------------      
    jQuery(".product-photos").each(function(){
      if (jQuery(".gallery-item img").length>1) {
        var prim=jQuery(".photo-primary img");
        jQuery(".gallery-item img").hover(function(){
          var newurl=jQuery(this).attr("src");
          prim.fadeOut("fast",function(){
           prim.attr("src",newurl);
           prim.fadeIn("fast");
          });
        },function(){}).click(function(){
          return false;
        });
      }
    });
    
    //----------------------------------    
    // Press page
    //---------------------------------- 
    jQuery("#press").each(function(){
        
      // Add hovers to press          
      jQuery(".press-item").each(function(){
        var th=jQuery(this);
        var tko=null;
        th.hover(
          function(){
            tko=window.setTimeout(function(){
              var prv=th.find(".popup");
              jQuery(".press-item").css("z-index",5);
              th.css("z-index",10);
              th.find(".popup").show("fast");
            },500)
          },
          function(){
            clearTimeout(tko);
            th.find(".popup").hide("fast");
          }
        );
      });
    
      //  Set up pager
      var rows=4;
      var currpg=0;
      var tbl=jQuery("#press table");
      var trs=jQuery("tr",tbl);
      var lastpg=Math.ceil((trs.length)/rows)-1;
      if (lastpg>0) {
         
        // Shift results and update pager - wait for callbacks
        function presspage(d) {
          if(currpg+d<0||currpg+d>lastpg)return false;
          currpg+=d;
	        currpg=(currpg<0)?0:((currpg>lastpg)?lastpg:currpg);
          tbl.hide("slow",function(){finishpresspage(d);});
        };
        function finishpresspage(d){
          var gt=(currpg*rows)-1;
          jQuery(".pager .previous").fadeTo("fast",((currpg>0)?1:.25));
          jQuery(".pager .next").fadeTo("fast",((currpg<lastpg)?1:.25));
          trs.hide();        
          trs.filter(":gt("+gt+")").filter(":lt("+rows+")").show();
          if(d!=0)tbl.show("slow");
        };
        //  Add pager and functions
        jQuery(this).append('<div class="pager">'+
	        '<a href="#" class="previous">&lt;&lt; Newer Press</a>'+
          '<a href="#" class="next">Older Press &gt;&gt;</a></div>');
        jQuery(".pager .previous").click(function(){presspage(-1);return false});
        jQuery(".pager .next").click(function(){presspage(1);return false});
        finishpresspage(0);        
      }
    }); // end     

    //----------------------------------    
    // Customize ents list
    //----------------------------------   
    jQuery(".cust-block-1 .multitabs").each(function(){

      // Override events
      tab_click_slide=function(hs,vs){
        if (hs.length>0) {
          hs.removeClass("active");
          hs.slideUp("slow",tab_click_slide_show);
        } else {
          hs.slideUp("slow",tab_click_slide_show);
        }
 
        // Internal function to complete load
        function tab_click_slide_show(){
          vs.slideDown("slow");
          vs.addClass("active");
        }
      }      
    });
     
    //----------------------------------    
    // Customize home/ent feature
    //----------------------------------      
    jQuery("#home-feature,#ent-feature").each(function(){
         
      // Override click events
      tab_click_slide=function(hs,vs){
        hs.removeClass("active");      
        if (hs.length>0) hs.fadeOut("slow",function(){vs.fadeIn("slow")})
        else vs.fadeIn("slow");
        vs.addClass("active");           
      }      
      tab_click_tab=function(ht,vt){
        ht.removeClass("active");
        ht.fadeTo("slow",.75);
        vt.fadeTo("slow",1);
        vt.addClass("active");
      }
      // Add previous/next function
      tab_nextprev=function(i){
        var ci=slides.index(slides.filter(':visible')[0]);
        if((ci+i)<0||(ci+i)>(tabs.length-1))return false;
        jQuery("a",tabs.eq(ci+i)).click();
        return false;
      }
      jQuery(".prev",multi).click(function(){return tab_nextprev(-1)});
      jQuery(".next",multi).click(function(){return tab_nextprev(1)});  
      // Set up   
      jQuery(".tab:not(.active)",multi).fadeTo("fast",.75);
      jQuery("#home-feature-tabs",multi).fadeIn("slow");
      jQuery("#home-feature-tab-buttons",multi).fadeIn("slow");
      jQuery("#ent-feature-tabs",multi).fadeIn("slow");
      jQuery("#ent-feature-tab-buttons",multi).fadeIn("slow");      
    });
    
});

