﻿var bbyon = {};
var img_Server="/bestbuy/scripts/bestbuy/bb_video.html";

bbyon = {
    thepage:{
        pagename:""
    },
   
    setthepage:function(pagetype){
        this.thepage.pagename = pagetype;     
    },
   
    preloadimg:function(imagearray){
        $.each(imagearray,function(e){
            jQuery('<img src="http://www.bestbuyon.com/' + this + '" />');
        });
    },
   
    init:function(query,page,template){
        document.getElementById('bbyon').style.display = '';

        $.ajaxSetup({
            "error":function() {
            $("#on-loading").html('<a href="http://www.bestbuyon.com" target="_blank"><img src="http://images.bestbuy.com/BestBuy_US/en_US/images/global/on-default.gif"></a>');
        }});
       
        $.ajax({
            url: "http://content.bestbuyon.com/solr/"+template+"/select/",
            global: false,
            type: "GET",
            data: ("q=tid%3A"+query+"%20AND%20type%3Akaltura_entry2&start=0&rows=3&indent=on&fl=title,ss_feature_desc,ss_kaltura_entryId,ss_field_bbydotcom_main_image,ss_field_bbydotcom_thumb_image&wt=json&qt=standard&sort=sis_field_bbydotcom_weight_"+page+"%20asc&json.wrf=bbyon.foo"),
            dataType: "jsonp",
            async:true
       });
       
       this.setthepage(page);
    },
   
    foo:function(data){
        if (data !== "object") {
            //It's not json response or is otherwise AFU, load default image
            $("#on-loading").html('<a href="http://www.bestbuyon.com" target="_blank"><img src="http://images.bestbuy.com/BestBuy_US/en_US/images/global/on-default.gif"></a>');
        }

        //first content object fields           
        var contentblocks = data.response.docs;
        var objectnum = contentblocks.length;
        var totalblocks = 3;
        var renderedhtml = '';
        var firstimg = data.response.docs[0].ss_field_bbydotcom_main_image;
        var firsttitle = data.response.docs[0].title;
        var firstdesc = data.response.docs[0].ss_feature_desc;
        var firstid = data.response.docs[0].ss_kaltura_entryId;
        var thepagename = this.thepage.pagename;
        
        //add output of first object to rendered HTML var
        renderedhtml += '<div id="upper"><a class="cbox" onclick="trackEvent.event(\'event.link\',{video:\'video launch\'})" href="' + img_Server + '?videoid='+firstid+'&name='+thepagename+'">';
        renderedhtml += '<img src="http://www.bestbuyon.com/' + firstimg + '" /></a></div><div id="description" style="border-left: solid 1px #dadada; border-right: solid 1px #dadada;">'+ firstdesc+'</div><div style="float: left; width: 298px; border-left: solid 1px #dadada; border-right: solid 1px #dadada; border-bottom: solid 1px #dadada;">';

        //new var      
        var bigimages = new Array();

        if(objectnum > 1){
            $.each(contentblocks, function(i, val){
                  var thumbimg = val.ss_field_bbydotcom_thumb_image;
                  var decodethumbimg = encodeURIComponent(val.ss_field_bbydotcom_thumb_image);
                  var thumbimg = new String(decodethumbimg);
                  thumbimg =  thumbimg.replace(/%2F/g,'/');
                                  
                  var thumbimg = val.ss_field_bbydotcom_thumb_image;
                  var decodebigimg = encodeURIComponent(val.ss_field_bbydotcom_main_image);
                  var bigimg = new String(decodebigimg);
                  bigimg =  bigimg.replace(/%2F/g,'/');
                                  
                  // each
                  //bigimages.push(bigimg);
                  if (typeof(bigimg) == 'string' && bigimg.length > 0) {
                         bigimages.push(bigimg);
                  }

                  renderedhtml += '<div class="thumbs box'+i+'"><a class="cbox" onclick="trackEvent.event(\'event.link\',{video:\'video launch\'})" href="' + img_Server + '?videoid='+val.ss_kaltura_entryId+'&name='+thepagename+'"><img class="thumb_img" src="http://www.bestbuyon.com/' + thumbimg + '" />';
                  renderedhtml += '<span>'+val.title+'</span></a></div>';              
                  
                  $(".thumbs").live('mouseover', function(){
                         if ($(this).hasClass("box" + i)){
                                 $("#upper").html('<a class="cbox" onclick="trackEvent.event(\'event.link\',{video:\'video launch\'})" href="' + img_Server + '?videoid='+val.ss_kaltura_entryId+'&name='+thepagename+'"><img src="http://www.bestbuyon.com/' + bigimg + '"></a>');
                                 $("#description").html(val.ss_feature_desc);                                                  
                         }       
                          $(".thumbs").find(".thumb_img").css("border-bottom","5px solid #fff");
                         $(".thumbs").find("span").css("text-decoration","none");
                         $(this).find(".thumb_img").css("border-bottom","5px solid #fff200");
                         $(this).find("span").css("text-decoration","underline");
                         
                         $('.cbox').click(function(){
                            $(".cbox").colorbox({width:"648", height:"468", iframe:true});
                         });
                  });
                          
                  $(".thumbs").live('mouseout', function(){
                         $(this).children("span").css("text-decoration","none");
                  });
            });     
        }
                   
        //preload images into cache outside of each loop
        bbyon.preloadimg(bigimages);

        renderedhtml += "<div id='view-more'><a href='http://www.bestbuyon.com?utm_campaign=SeeMore&utm_medium=Link_BB&utm_source=bestbuy.com&utm_content=Module' target='_blank'>See more at Best Buy On &rsaquo;</a></div></div>";

        $("#on-loading").html(renderedhtml).fadeIn("slow");

        $('.cbox').click(function(){
            $(".cbox").colorbox({width:"648", height:"468", iframe:true});
        });               
    }               
}
