// Xerox scripts file
$(function(){
 // left bar expands
 $("#leftBar .exp").click(function(){
  $(this).toggleClass("exp-opened")
   .next("ul").toggle();
  
  return false;
 });
 
 // content recent stripes
 $(".kioskRecent .newsBody:odd").addClass("newsBody-color");
 
 //related releases kiosk
 var kiosk_rel = $(".kioskRelated");
 if (kiosk_rel.length) {
  kiosk_rel.prCurrent = kiosk_rel.attr("current");          // current release;
  kiosk_rel.prMax = parseInt(kiosk_rel.attr("max"));        // total to show
  
  $(".newsBody[release='" + kiosk_rel.prCurrent + "'], .newsBody:eq("+kiosk_rel.prMax+")", 
    kiosk_rel).eq(0).hide();
  // have to use filter() due to FF3.0  
  if ($(".newsBody", kiosk_rel).filter(function(){return !$(this).attr("style")}).length)
   kiosk_rel.show();
 }

 // todo: make same for images and videos after launch
 // related images handling
 $(".newsImage a, h4 a", ".kioskImages").click(function(){
   prShowModal(this.href); 
   return false;
 });
 
 // related videos handling 
 $(".viewVideos").click(function(){
  prShowModal(this.href);
  return false;
 });
 
 // carousel
 prInitCarousel();

 // paging style
 prPagingStyle();
 
 // rounded corners 
 prRounded();
 
 //external link/pdf files
 $("#content a").prExternal("pdf");
 
 // hide the first item of IR In The Spotlight Bin
 $(".binRecentIR .newsBody:first").addClass("first");
 
 // seprate events list into upcoming and past
 eventsListInit();
})


// styling of paging
function prPagingStyle(){
  $(".prPaging form").each (function(){
   $(this).prev("a").html('<img src="images/btn_prev.gif" alt="prev" />').show();
   $(this).next("a").html('<img src="images/btn_next.gif" alt="next" />').show();
  });
  $(".prPaging form").submit(function(){
   var pg_index = parseInt(this.pg.value);
   
   // validation
   if ( !(pg_index > 0 && pg_index <= prPagingTotal) ) {
    alert("Value out of range");
    return false;
   }
  
   var a = $("a", this)[0];
   if (a) {
    // gallery paging
    a.href = a.href.replace(/pg=\d+/i, "pg="+pg_index);
    $(a).click();
   }
   else {
    // non-gallery paging
    location.href = this.action.replace(/pg=\d+/i, "pg="+pg_index);
   }

   return false;
  })
  
  // prPagingTotal is defined on page
  if (typeof(prPagingTotal) != "undefined")
   $(".prPaging input").after(" of " + prPagingTotal); 
};


// show xerox modal window
function prShowModal(url){
 $("#prModal .prModalContent").load(url, function() {
  $("#prModal").modal();
  rocon.update($("#prModal .ePlayer")[0]);
 });
}

// seprate events list into upcoming and past
function eventsListInit() {
 // upcoming events
 $(".eventsList .newsBody")
  .filter(function(){
   var today = new Date();
   
   var d = new Date($(".newsDate", this).html());
   d.setHours(23);
   d.setMinutes(59);
   return (d.getTime() - today.getTime()) > 0;
  })
  .wrapAll('<div class="kiosk kioskRecent kioskRecentIR"></div>')
  .parent().prepend('<h3>Upcoming Events</h3>');
 
 // past events
 $(".eventsList>.newsBody")
  .wrapAll('<div class="kiosk kioskRecent kioskRecentIR"></div>')
  .parent().prepend('<h3>Previous Events</h3>');
}


// init carousel
function prInitCarousel() {
 if (!$("#carousel").length)
  return;

 // init
 $("#carousel").jCarouselLite({
     btnNext: ".next",
     btnPrev: ".prev",
     visible: 1
     ,auto: 5000
     ,btnGo: [".carouselNav .c1", ".carouselNav .c2", ".carouselNav .c3"]  
     ,afterEnd: function(a) {
       var i = $("#carousel li").index(a[0]);
       // trick to fix index, for 3 visible items use 5 li
       i = (i==4) ? 1 : i;
      
       if (i>-1) {
        $(".carouselNav a").removeClass("active").eq(i-1).addClass("active");
       }
     }
     ,beforeGo: function() {
      $("#carousel").jCarouselLite.stop();
      $(".carouselNav .cplay").removeClass("cpause");
     }
       
 });
 
 // play/pause button
 $(".carouselNav .cplay").click (function() {
  if ($(this).hasClass("cpause"))
    $("#carousel").jCarouselLite.stop(); 
  else
    $("#carousel").jCarouselLite.play()
  $(this).toggleClass("cpause");

  return false;
 })
 .addClass("cpause");
 
 // 1st item
 $(".carouselNav li:first a").addClass("active");
}


// Rounded corners
function prRounded() {
 $(".pr-rounded").each(function(){
  if ($("img, object, embed", this).length) {
    var w = $(this).wrapInner('<div class="pr-rounded-wrap"></div>')
    .children(".pr-rounded-wrap")
    .prepend('<div class="tl"><div class="tr"></div></div>')
    .append('<div class="bl"><div class="br"></div></div>');
  } 
 })
}

// HBX tracking call for downloads
function pr_hbxTracking(s) {
 if (typeof _hbDownload != "undefined" ) {
  _hbDownload ("/corp/us/download/" + s.replace (/.*\//g, "") );
 }
}

