var thisLoc = location.host;
var sep = thisLoc.indexOf(".");
var pre = thisLoc.substr(0,sep);

//Omniture tracking variables
var jsaccount="agdev";
var jschannel="";
var jspageName="";
var jsbrand="";
var jssection="";
var jsprop2="";
var jsprop3=""; 
var jsprop4="";
var jsprop5="";
var jsprop6="";
var imghost="";
var khost="";
var ahost="";
var shost="";


jsaccount = pre.indexOf("www") == 0 ? "agagkidzone" : "agdev";

//Tracking for a src paramater
var src_code = jQuery.url.param("src");
if (src_code != '')
{
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    jsprop3 = sPage + "|" + src_code; 
}
//End tracking vars


var isHH = false;
var isCBbaby = false;
//This function chooses which video to show on HH about page.
function setup_HH_vp(click_name){
    var flashvars = {};
    if(click_name == "#frstplay"){
            flashvars = {
                prefix: "www",
                assetPath: imghost+"/agkidzone/swf/parents/flv/int_leann_vi_hh.flv",
                assetTitle: "A Chat with LeAnn Rimes",
                description: "Find out why Holly has LeAnn Rimes singing!",
                autostart: "true"
            };
            $("#HHdialog-modal").attr("title","A Chat with LeAnn Rimes");
    }
    else if(click_name == "#sndplay"){
            flashvars = {
                prefix: "www",
                assetPath: imghost+"/agkidzone/swf/parents/flv/twinkleinhereye_vi_hh.flv",
                assetTitle: "Listen to LeAnn Rhimes",
                description: "Twinkle in her eye",
                autostart: "true"
            };
            $("#HHdialog-modal").attr('title','Listen to LeAnn Rhimes');
     }
     else if(click_name == "#thrdplay"){
            flashvars = {
               prefix: "www",
               assetPath: imghost+"/agkidzone/swf/parents/flv/int_holly_vi_hh.flv",
               assetTitle: "Meet the Real Holly Hobbie",
               description: "An interview with the original creator of Holly Hobbie!",
               autostart: "true"
            };
            $("#HHdialog-modal").attr("title","Meet the Real Holly Hobbie");
      }
      var params = {
          base: imghost+"/agkidzone/swf/",
          allowscriptaccess: "always"
      };
     var attributes = {
         id: "vidplayer"
     };
     swfobject.embedSWF(imghost+"/agkidzone/swf/parents/miniPlayer.swf", "player", "500", "400", "9.0.0", imghost+"/agkidzone/swf/expressinstall.swf", flashvars, params, attributes);
}

//Loads Care Bears Baby Nursery into the pd
function setup_CBbaby_nursery(){
     var flashvars = { };
     $("#cbbabydialog").attr("title","Care Bears Nursery");
     var params = {
          base: imghost+"/agkidzone/swf/parents/cb_nursery/",
          scale: "noscale",
          allowscriptaccess: "always"
     };
     var attributes = {
         id: "cb_nurseryjs"
     };
     swfobject.embedSWF(imghost+"/agkidzone/swf/parents/cb_nursery/shell.swf", "nursery", "786", "480", "9.0.0", imghost+"/agkidzone/swf/expressinstall.swf", flashvars, params, attributes);
}

//Care Bears Baby Nursery swf calls this when it needs to open the Walmart page
function walMartOpen(url){
        var cbinfant = window.open(url,"WalMart","toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes");
    }
//This function is a universal dialog builder using jQuery
function build_dialog(dialog_name, click_name, hght, wdth)
{
   if(isHH){
      setup_HH_vp(click_name);
   } 
   if(isCBbaby){
      setup_CBbaby_nursery();
   }
    $(dialog_name).dialog({ 
        autoOpen: false,  
        modal: true,
        height: hght,
        width: wdth,
        position: "center",
        closeOnEscape: true,
        close: function(ev, ui){
               if(isHH){
                  $("#vidplayer").remove();
                  $(dialog_name).html("<div id='player'></div>");
               }
               if(isCBbaby){
                  $("#cb_nurseryjs").remove();
                  $(dialog_name).html("<div id='nursery'></div>");
               }
               $(this).dialog("destroy");

            }
    });
    $(click_name).click( function() { 
            build_dialog(dialog_name, click_name, hght, wdth); 
            $(dialog_name).dialog('open');
        } 
    );
}

//HH Dialog Boxes
function setup_HH_dialogs() {   
  var hgt = 0;
  var wth = 0;
  if(check_for_flash("#HHdialog-modal", "player")){
     isHH = true;
     hgt = 500
     wth = 600 
  }
  else{
     isHH = false
     hgt = 300;
     wth = 630;
  }  
  build_dialog('#HHdialog-modal','#frstplay', hgt, wth);
  build_dialog('#HHdialog-modal','#sndplay', hgt, wth);
  build_dialog('#HHdialog-modal','#thrdplay', hgt, wth);
}
//Check for flash for the dialogs
function check_for_flash(dialog_name, id){
    var hasFlash = detectFlash();
    if (hasFlash == false){
        showNoFlash(dialog_name, id);
    }
    return hasFlash;
}
//Show this image if there is no flash
function showNoFlash(dialog_name, id){
   $('#' + id).remove();
   $(dialog_name).html("<div id='"+id+"'><a href= 'http://get.adobe.com/flashplayer/'><img src=" +  imghost + '/agkidzone/agkz_noflash.gif \></a></div>'); 
}

//SP Dialog Boxes
function setup_SP_dialogs() {
  build_dialog('.IMdialog-modal','#IMlink', 400, 650);
  build_dialog('.KAdialog-modal','#KAlink', 400, 650);
  build_dialog('.MMdialog-modal','#MMlink', 400, 650);
  build_dialog('.TMdialog-modal','#TMlink', 400, 650);
  build_dialog('.WPdialog-modal','#WPlink', 400, 650);
  build_dialog('.SPdialog-modal','#SPlink', 400, 650);
}
//Care Bears Baby dialog box
function setup_CBbaby_dialog() {
  var hgt = 0;
  var wth = 0;
  if(check_for_flash("#cbbabydialog", "nursery")){
     isCBbaby=true;
     hgt = 550
     wth = 850 
  }
  else{
     isCBbaby=false;
     hgt = 300;
     wth = 630;
  }  
  build_dialog('#cbbabydialog','#cbbabyclick', hgt, wth);
}

//HH Accordian collapsible for Alyson Stoner
var HHAIsShown = false;
function accordionFunc(){
    if(HHAIsShown){
      $("#descblock").css('display','none');
      HHAIsShown=false;
    }
    else{
      $("#descblock").css('display','block');
      HHAIsShown=true;
    }
}

// Shop products catalog logic
var current_page = 1, products_per_page = 5,
    total_pages, total_products;

function initialize_catalog() {
    total_products = products.length;
    total_pages = Math.ceil(
        total_products / products_per_page);
    if(total_pages > 0) {
        show_catalog();
    }
}

function show_catalog() {
    update_paging();
    build_and_display_page();
}

function update_paging() {
    $('#paging_navigation').html([
        current_page == total_pages ?
        '<div id="next_page_greyed_out"></div>' :
        '<a href="#" id="next_page" onclick="return next_page()"></a>',

        '<div id="page_numbers">Page ',
        current_page, ' of ', total_pages, '</div>',

        current_page == 1 ?
        '<div id="back_page_greyed_out"></div>' :
        '<a href="#" id="back_page" onclick="return back_page()"></a>'
    ].join(''));
}

function build_and_display_page() {
    var html_parts = [], start, end, i, product;

    start = (current_page - 1) * products_per_page;
    end = total_products - start <= products_per_page ?
        total_products : start + products_per_page;

    for(i = start; i < end; i++) {
        product = products[i];
        html_parts.push([
            '<div class="product">',
            '<img src="', imghost, product.thumb_url, '"/>',
            '<h1>', product.title,'</h1>',

            (product.description != 'N/A' ?
                ['<h2>', product.description, '</h2>'].join('') : ''),

            '<h3>', product.copytext, '</h3>',

            '<h4>',
                product.vendor_links.length > 0 ?
                build_vendor_links_html(product.vendor_links) : 
                'Available in store at mass retailers',
            '</h4>',

            '</div>'
        ].join(''));
    }
    $('#products').html(html_parts.join(''));
}

function build_vendor_links_html(vendor_links) {
    var html_parts = [], link, i;

    for(i = 0; i < vendor_links.length; i++) {
        link = vendor_links[i];
        html_parts.push([
            '<a href="', link.url, '" target="_blank" class="',
                link.classname, '"></a>',
        ].join(''));
    }

    return html_parts.join('');
}

function next_page() {
    current_page++;
    show_catalog();
    return false;
}

function back_page() {
    current_page--;
    show_catalog();
    return false;
}
