//Homepage Image Switching
//Required homeImages.xml

//XML Example:
/*
<homeImages>
<imgswap id="homeImg1" swaptime="5">
 <image link="/somepage.html">/images/ex1.jpg</image>
 <image>/images/ex2.jpg</image>
</imgswap>
<imgswap id="homeImg2" swaptime="5">
 <image link="/somepage.html">/images/ex1.jpg</image>
 <image>/images/ex2.jpg</image>
</imgswap>
</homeImages>
*/

//CSS Needed
/*
.imgSwapHidden{ display:none; } 
*/

//This should be in a packed or minified common.js file 
if(typeof("$.xmlToJSON")!="function")
 document.write("<script language='javascript' type='text/javascript' src='" + webPath + "/jscript/jqXMLUtils.pack.js'></script>");

var imgSwapObj;

$(document).ready(function() {
  $.ajax({
    type: "GET",
    url: webPath + "/homeImages.xml",
    dataType: "xml",
    complete: function(data) { 
       imgSwapObj = $.xmlToJSON(data.responseXML);
       if(typeof(imgSwapObj.imgswap)!="undefined" && typeof(imgSwapObj.imgswap)!="null"){ // Prevent G is undefined errors if XML is incorrect
         $.each(imgSwapObj.imgswap,function(i){
           if(typeof("this.image")!="undefined" && typeof("this.image")!="null"){ // Prevent G is undefined errors if XML is incorrect
             $.each(this.image,function(j){
              this.href[0].Text = this.href[0].Text.replace(/\'/g,'"'); //jQuery wraps attrib with ' not " so must replace signle quotes with double quotes if using javascript in href
               if(j==0)
                 $("#" + imgSwapObj.imgswap[i].id).html("<a href='" + this.href[0].Text + "'><img src='" + this.src[0].Text + "' /></a>")
                                                  .data("curIndex",0)
                                                  .data("imgswap",imgSwapObj.imgswap[i]);   
               else if(j==1){
                 vSwapHTML =  "<div id='" + imgSwapObj.imgswap[i].id + "-back'";
                 vSwapHTML += "style='position:relative;z-index:0;'";               
                 vSwapHTML += "><a href='" + this.href[0].Text +"'><img src='" + this.src[0].Text + "' /></a></div>";
                 $("#" + imgSwapObj.imgswap[i].id).after(vSwapHTML);                 
                 $("#" + imgSwapObj.imgswap[i].id).css("position","absolute").css("z-index","5");
               } 
             }); // each image
             vImgTimer = setTimeout("switchBanner('#" + imgSwapObj.imgswap[i].id + "')",imgSwapObj.imgswap[i].swaptime * 1000);
           }
         }); // each imageswap
       }
    }
  });
});

function switchBanner(vTarget) {
  //TODO: Store Dom element vTarget in var to reuse
  $(vTarget).fadeOut(1000,function(){ 
    var vSwap = $(vTarget).data("imgswap"); // Get imageSwap object stored on element
    var vCurIndex = $(vTarget).data("curIndex");
    if(vCurIndex < vSwap.image.length - 1)
      vCurIndex++;
    else
      vCurIndex = 0;
    $(vTarget).data("curIndex",vCurIndex);
    $(this).html($(vTarget + "-back").html());
    $(this).css("display","block");
    $(vTarget + "-back a").attr("href",vSwap.image[vCurIndex].href[0].Text);
    $(vTarget + "-back img").attr("src",vSwap.image[vCurIndex].src[0].Text);
    vImgTimer = setTimeout("switchBanner('" + vTarget + "')",vSwap.swaptime * 1000);
  });
}
