/**
 * @author Tanivova
 */
//Informer in the footer and popUp image for tv
var betterUI = {
    init : function () {
	    $("<div id='imgHolder'><img src=''/></div>").hide().appendTo('body');
		$.get('/files/informer.xml',function (data){
	        //betterUI.informer(data);
			betterUI.popUp(data);
	    });
    },
	informer : function (itx) { 
        $('div#footer').empty();
	    var maxLength =4 ;
	    var infLength = $(itx).find('item').length -1;
	    var chosen = new Array();
	    do{
	  	    var newVal = Math.floor(Math.random()*infLength);
			if($.inArray(newVal,chosen) < 0)
			    chosen.push(newVal);
	  	} while(chosen.length < maxLength);
				
	    var htm = "";						 
	   $.each(chosen,function(key,value){
		   var item = $(itx).find('item').eq(value);
		   var image =item.find('image').text();
		   var url = item.find('url').text();
		   url += "tease/";
		   var name = item.find('name').text();
		   var description = item.find('description').text().substr(0,70)+"...";
		   description = description.replace(/quot;/, "'").replace(/ndash;/, "-").replace(/nbsp;/, " ").replace(/rsquo;rsquo;/, '"');
 		   var price = item.find('price').text();
		   htm += '<div class="infoMain hproduct"><div class="infoImage"><a href="';
		   htm += url;
		   htm += '" ><img width="90px" src="';
		   htm += image;
		   htm += '" alt="';
		   htm += name;
		   htm += '" class="photo"/></a><a class="a" href="';
		   htm += url;
		   htm += '" ">подробнее</a></div><div class="infoTxt"><h2 class="fn"><a href="';
		   htm += url;
		   htm += '" title="';
		   htm += name;
		   htm += '" class="url">';
		   htm += name;
		   htm += '</a></h2><p class="description">';
		   htm += description;
		   htm += '</p><span class="price">';
		   htm += price;
		   htm += ' </span></div></div>';
	  });
	$('div#footer').css({'overflow':'hidden','height':'160px','padding-left':'10px'}).html(htm);
	$('div#footer div.infoMain').css({'float':'left','width':'23%','height':'120px','margin':'5px', 'border-color':'#b1b1b1','border-style':'solid','border-width':'1px'});
  },
popUp : function (tvs) {
	    $("#leftSidebar div.sideBlockContent ul li a, #rightSidebar div.sideBlockContent ul li a, .m_blck1 a, .m_blck2 a.a1")
	        .hover( function (){
			    var id = $(this).text().split(': ').pop();
				
                var imgSrc = $(tvs).find('item[id~='+id+']').find('image').text();
				if(imgSrc){
	                $(this).attr('title','');
	            	    var top = $(this).position().top;
	            	    if($(this).parent().position().left < 700){
	            	        var left = $(this).parent().position().left + $(this).parent().width() + 30;
	            	    } else {
	            		    var left = $(this).parent().position().left - 130;
	            	    }
					   $("div#imgHolder img").attr("src",imgSrc);
					   $("div#imgHolder").css( {"top":top,"left":left}).show();
					}
	            }, 
	            function () {
	                $("div#imgHolder").hide();
					
	            });
		}	
};
//GA goals for step 1
GATC = {
	addGoalsStep1 : function (){
		$('a[href*="shopcart"]').bind('click',function(){pageTracker._trackPageview('/buy.html')});
        $('a[href*="send_pro.php"]').bind('click',function(){pageTracker._trackPageview('/avail.html')});
        $('a[href*="send_pro_price.php"]').bind('click',function(){pageTracker._trackPageview('/price.html')});
	}
};
$(document).ready(function(){
	try {
	 GATC.addGoalStep1();
	} catch (e) {}
	try {
	   betterUI.init();
    } catch (e) {}
});
