// 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;
 });
 
 // paging style
 prPagingStyle();
 
 // rounded corners 
 prRounded();
})


// 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(prPagingPre) == "undefined")
   prPagingPre = "of";
  if (typeof(prPagingTotal) != "undefined")
   $(".prPaging input").after(" "+prPagingPre+" " + prPagingTotal); 
};


// show xerox modal window
function prShowModal(url){
 // create own copy of modal window
 if (!$("#prModal").length) {
   var m = $("#country_selector_modal_hdr").clone();
   m.attr("id", "prModal").removeAttr("style").hide()           
    .find("#country_selector_modal_hdr_iframe").attr("id", "prModal_frame")
    .removeAttr("style").removeAttr("width").removeAttr("height")
   m.appendTo("body");
 }
 
 $("#prModal_frame").load(url, function() {
  $("#prModal").modal({close: false});
  rocon.update($("#prModal_frame .ePlayer")[0]);
 });
}


// 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, "") );
 }
}

