// BEGIN BREAK OUT OF FRAMES

function bustFrames(){
  if (window != top){
    top.location.href=location.href
  }
}

// END BREAK OUT OF FRAMES



// BEGIN FIELD-CLEARING FUNCTION

function clearField(field){
  if (field.value == field.defaultValue) {
    field.value = "";
  }
}

// END FIELD-CLEARING FUNCTION

// BEGIN DHTML FUNCTIONS

// initRollovers - Find all images that have the class "roll", and set events for each (avoids needing to add event handling to image tags)
// - loops through all image tags on the page, and skips any that have no 'roll' class

function initRollovers()
{
  // Set rollstate variable (this is typically either 'on' or 'over')
  var roll_state = "over";
  // Loop through all im if they contain the class roll
  var imgs=document.getElementsByTagName('img');
  for(i=0;i<imgs.length;i++){
    img_obj = imgs[i];
    if(/roll/.test(img_obj.className)){

      // add the function roll to the parent Element (i.e a href) of the image
      if(/nav/.test(img_obj.className)){
        img_obj.parentNode.onmouseover=function(){rollOver(this, roll_state);showSection(this);};
        img_obj.parentNode.onmouseout=function(){rollOver(this, '');hideSection(this);};
      }
      else if(/sub/.test(img_obj.className)){
        img_obj.parentNode.onmouseover=function(){rollOver(this, roll_state);showSection(this);};
        img_obj.parentNode.onmouseout=function(){rollOver(this, '');hideSection(this);};
      }
      else{
        img_obj.parentNode.onmouseover=function(){rollOver(this, roll_state);};
        img_obj.parentNode.onmouseout=function(){rollOver(this, '');};
      }
      img_obj.parentNode.onfocus=function(){rollOver(this, roll_state);};
      img_obj.parentNode.onblur=function(){rollOver(this, '');};
      // Preload images (on and over variations)
      img_width = img_obj.width;
      img_height = img_obj.height;
      img_name = img_obj.id;
      img_ext = img_obj.src.substring(img_obj.src.lastIndexOf('.'), img_obj.src.length);

      // Preload 'over' images (if applicable)
      img_name_over = img_name.replace("_button", "_over");
      eval (img_name_over + " = new Image(" + img_width + "," + img_height + ")" );
      eval (img_name_over + ".src = \"" + img_obj.src.replace(img_ext, "_over" + img_ext) + "\"");

      // Preload 'on' images (if applicable)
      img_name_on = img_name.replace("_button", "_on");
      eval (img_name_on + " = new Image(" + img_width + "," + img_height + ")" );
      eval (img_name_on + ".src = \"" + img_obj.src.replace(img_ext, "_on" + img_ext) + "\"");

      //      showObjectProperties(img_obj.parentNode);
    }
  }
}

// initSmartNav - Highlights all navigation for current page (at each level)
// - loops through each part of path (parts = directory or filename)

function initSmartNav(){
  js_server_path = new String(location.pathname);
  // Specify filename suffix for highlighted nav
  var hl_suffix = "_on";
  delete js_pathparts;
  var js_pathparts = js_server_path.split("/");
  
  if (location.pathname.indexOf("product_list") != -1 && location.pathname.indexOf("search.aspx") == -1){
    if (location.pathname.indexOf(/\?state=/) != -1 || location.pathname.substring(location.pathname.length - 1) == "/"){
         var querystring = location.search.split("&")
         if (querystring[0]){
           js_pathparts[3] = querystring[0].replace(/\?state=/,"");
         }
         else{
           js_pathparts[3] = "category";
         }  
         if (querystring[1]){  
           js_pathparts[4] = querystring[1].replace(/subState=/,"");
         }
         else{
           if (js_pathparts[3] == 'category'){
             js_pathparts[4] = 'dvds';
           }
           if (js_pathparts[3] == 'age'){
             js_pathparts[4] = 'mos0';
           }
           if (js_pathparts[3] == 'theme'){
             js_pathparts[4] = 'music';
           }
         }
     }
     else
     {
        if (js_pathparts[4]){
           js_pathparts[3] = js_pathparts[4];
           js_pathparts[4] = js_pathparts[5].replace(".html", "");
         }
         else{
           js_pathparts[3] = "category";
           js_pathparts[4] = "dvds";
         }
     }
     
     hl_suffix = "_over";
  }
  //if (location.pathname.indexOf("/sp/") != -1){
  //  if (js_pathparts[2]){
  //    js_pathparts[3] = js_pathparts[2] + "_sp";
  //  }
  //  js_pathparts[2] = "espanol";
  //  hl_suffix = "_on";
 ///}

  // Loop through path parts to find navigation matches
//  for(i=0;i<js_pathparts.length;i++){
  for(i=2;i<=4;i++){ // In this case only use element 2 for highlighting (loop kept here in case it is needed for subnav highlighting)
    if (js_pathparts[i]){
      pathpart = js_pathparts[i];
      // If pathpart is not empty and if element exists with pathpart and '_button' as it's ID
      if (pathpart.length > 0 && document.getElementById(pathpart+'_button')){
        navbutton = document.getElementById(pathpart+'_button');
        // If navbutton doesn't already include hl_suffix in the filename add it (new src = everything before the period, then hl_suffix, then everything after)
        if (navbutton.src.indexOf(hl_suffix) == -1 && navbutton.className != 'sub_roll'){
          img_ext = navbutton.src.substring(navbutton.src.lastIndexOf('.'), navbutton.src.length);
          navbutton.src= navbutton.src.replace(img_ext, hl_suffix+img_ext);
        }
        if (i == 4){
          disable_rollover_effects(navbutton.parentNode);
        }
      }
    }
  }
}

function disable_rollover_effects(butt){
  // Disable rollover effects for highlighted nav
  butt.parentNode.onmouseover=null;
  butt.parentNode.onmouseout=null;
  butt.parentNode.onfocus=null;
  butt.parentNode.onblur=null;
}

function rollOver(obj, rollstate){
  var i,isnode,src,ftype,newsrc,nownode;
  // loop through all childNodes (i.e. img within a)
  for (i=0;i<obj.childNodes.length;i++){
    nownode=obj.childNodes[i];
    // if the node is an element and an IMG set the variable and exit the loop
    if(nownode.nodeType==1 && /img/i.test(nownode.nodeName)){
      isnode=i;
      break;
    }
  }
  // check src and do the rollover
  img_src = obj.childNodes[isnode].src;
  ftype = img_src.substring(img_src.lastIndexOf('.'), img_src.length);
  if(/_on/.test(img_src)){
    currstate = 'on';
  }
  else if(/_over/.test(img_src)){
    currstate = 'over';
  }
  else{
    currstate = '';
  }
  newsrc = '';
  if (currstate != rollstate){
    newsrc = img_src.replace(ftype, '_'+rollstate+ftype);
  }
  else{
    return;
  }
  if (rollstate.length == 0){
    newsrc = img_src.replace('_'+currstate, '');
  }
  if (newsrc.length != 0){
    obj.childNodes[isnode].src=newsrc;
  }
}

// END DHTML FUNCTIONS

// WINDOW FUNCTIONS
function loadPopup(url, name, width, height, chrome, scrollbars, resizable, centered) {
  // For chrome, scrollbars, resizable, centered use 1 or 0
  if (centered == 1){
    var winleft = Math.round((screen.width-width)/2);
    var wintop = Math.round((screen.height-height)/2);
    if (chrome == 1){
      wintop = wintop - 80;
    }
    else{
      wintop = wintop - 30;
    }
    if (wintop < 0) wintop = 0;
  }
  var features = '';
  features = features + 'width=' + width;
  features = features + ',height=' + height
  if (chrome == 1){
    features = features + ',directories=1,location=1,menubar=1,status=1,toolbar=1';
  }
  else{
    features = features + ',directories=0,location=0,menubar=0,status=0,toolbar=0';
  }
  features = features + ',resizable=' + (resizable - 0);
  features = features + ',scrollbars=' + (scrollbars - 0);
  if (winleft > 0 && wintop > 0){
    features = features + ',left=' + (winleft - 0);
    features = features + ',top=' + (wintop - 0);
  }
  //alert("Features: " + screen.width + "/" + screen.height + " -- " +features);
  popup = window.open (url, name, features);
  popup.focus();
  return void(0);
}

// To close a window use... window.close();
// To print use... window.print();
// To redirect use... window.location = "http://www.google.com/";


// BEGIN NAV SUB DROPDOWN


var navsections = new Array();
navsections[0] = "nav_sub_products";
navsections[1] = "nav_sub_our_story";
navsections[2] = "nav_sub_hot_topics";
navsections[3] = "nav_sub_worldwide";


function showSection (obj) {
  var tag_name = obj.name;
  if(/-/.test(tag_name)){
    tag_name = tag_name.substring(0,tag_name.lastIndexOf('-'));
  }
  var z = 0;
  for (z = 0; z < 4; z ++) {
    if ("nav_sub_"+tag_name == navsections[z]) {
      MM_showHideLayers(navsections[z],'','show');
    }
    else {
      MM_showHideLayers(navsections[z],'','hide');
    }
  }
}


function hideSection (obj) {
  var tag_name = obj.name;
  if(/-/.test(tag_name)){
    tag_name = tag_name.substring(0,tag_name.lastIndexOf('-'));
  }
  var z = 0;
  for (z = 0; z < 4; z ++) {
    if ("nav_sub_"+tag_name == navsections[z]) {
      MM_showHideLayers(navsections[z],'','hide');
    }
  }
}




function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
    else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
  if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
  obj.visibility=v; }
}



// END NAV SUB DROPDOWN


// Begin Show and Hide Layers

function showLayer (layer_id) {
  document.getElementById(layer_id).style.display='block';
}
function hideLayer (layer_id) {
  document.getElementById(layer_id).style.display='none';
}

// End Show and Hide layers

// For Debugging
function showObjectProperties(obj){
  var temp = "";
  for (x in obj){
    //    if (x == 'onMouseOver'){
    temp += x + ": " + obj[x] + "               ";
    //    }
  }
  alert ("debug" + temp);
}

// Function for toggling comments
comment_pointer = 0;
function switch_comment(direction)
{
    if (direction == "next")
    {
        if (!comment_array[comment_pointer+1])
        {
            return;
        }
        comment_pointer += 1;
    }
    if (direction == "prev"){
        if (!comment_array[comment_pointer-1]){
            // We're at the first item
            // Turn off 'prev' link
            document.getElementById("commentPrevButton").style.display='none';
            return;
        }
        else{
            // Turn on 'next' link
            document.getElementById("commentNextButton").style.display='block';
            // Turn on 'prev' link
            document.getElementById("commentPrevButton").style.display='block';
        }
        comment_pointer -= 1;
    }
    document.getElementById("commentHTML").innerHTML = comment_array[comment_pointer];
    document.getElementById("userinfoHTML").innerHTML = userinfo_array[comment_pointer];
    if (!comment_array[comment_pointer+1])
    {
        // Turn off 'next' link
        document.getElementById("commentNextButton").style.display='none';
    }
    else{
        // Turn on 'next' link
        document.getElementById("commentNextButton").style.display='block';
    }
    if (!comment_array[comment_pointer-1])
    {
        // Turn off 'prev' link
        document.getElementById("commentPrevButton").style.display='none';
    }
    else{
        // Turn on 'prev' link
        document.getElementById("commentPrevButton").style.display='block';
    }
}