var spinnerStr = "<img src=/Images/spinner.gif align=abssmiddle>"


function addProductToCompare(url){
	createMessage("Минуточку..." + spinnerStr, 500);
	SetRequestSend(url);
}
function cleanCompare(obj){
	createMessage("Минуточку..." + spinnerStr, 500);
	var url = 'http://' + document.domain + '/short.php?action=Compare&clean=yes';
	SetRequestSend(url);
}
function SetRequestSend(url){
	var obj = document.getElementById('comparecont');
	var request = createRequestObject();
	request.open('GET', url, true);
	request.onreadystatechange = function() {
	    if (request.readyState == 4) {
		if(request.status == 200) {
		    obj.innerHTML = request.responseText;
		    setTimeout("hideMessage()", 500);
		    request.abort();
	        }
	    }
	};
	request.send(null);
}
function createRequestObject() {
        var xmlhttp;
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
    	    try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
        	xmlhttp = false;
            }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    	    xmlhttp = new XMLHttpRequest();
        }
        return xmlhttp;
    }
    
function compare_show(vals, meth){
	var cv = document.getElementById('compareview');
	var enable = true;
	if (meth == 1){
	    if (!cv) return;
	    var cv_inputs = cv.getElementsByTagName('input');
	    if (!cv_inputs) return;
	
	    for (i=0; i<cv_inputs.length; i++){
		if (cv_inputs[i].getAttribute('value') == vals){
		    if (cv_inputs[i].checked === true) enable = false;
		    cv_inputs[i].checked = true;
		}
		else{
		    cv_inputs[i].checked = false;
		}
	    }
	}
	
	if (enable === true){
	    var newstring;
	    var newlocation = window.location.toString();
	    var newsearch = window.location.search;
	    newlocation = newlocation.replace(/view_(yes|no)\//, '');
	    newlocation = newlocation.replace(newsearch, '');
	    switch (vals) {
		case 2:
		    newstring = 'view_no/';
		break;
		case 3:
		    newstring = 'view_yes/';
		break;
		default:
		    newstring = '';
	    }
	    
	    newlocation = newlocation + newstring + newsearch;
	    window.location = newlocation;
	}
}
function ShowBasketWindowNew(url){
//	alert(url);
	createMessage("Минуточку..." + spinnerStr + "<br><br><span>Товар добавляется в корзину</span>", 500);
	$.get(url,{}, function(txt){
	    $("#shortbasket").html(txt);
	    setTimeout("hideMessage()", 800);
	    $("#gotobasket:hidden").css({top:$(window).scrollTop()}).fadeIn();
//	    $(window).trigger('scroll');
	});
}

function hideMessage()
{
    if ($("#mess").length){
	$("#mess").remove();
    }
}
function createMessage(mess_text)
{
    $("body").append('<div id="mess" style="padding: 15px;"></div>');
    $("#mess").html(mess_text);
    $("#mess").css({top: ($(window).scrollTop() + 200), left: ($(window).width()/2 - $("#mess").width()/2)});
}

function startEffects(){
//	onButtonAll();
}
function CreateLinkText(mess_text){
    var LC = document.getElementById('LinkCompare');
    var mess_div = document.getElementById('mess');
    var d = '<div align="center"><input type="button" value="закрыть" onclick="hideMessage()"></div>';
    var br=LC.getBoundingClientRect()
//    alert("Top:"+br.top+", Left:"+br.left+", Right:"+br.right+", Bottom:"+br.bottom)
    mess_div.style.visibility = "visible";
    mess_div.innerHTML = '<div id="copytext" onclick="TextSelect(this)">' + mess_text + '</div>' + d;
    mess_div.style.top = br.bottom + 5;
    mess_div.style.left = br.left;
    mess_div.style.padding = "15px 10px";
    var divcopy = document.getElementById('copytext');
    divcopy.style.whiteSpace = "nowrap";
    divcopy.style.paddingBottom="10px";
    
    
}
function TextSelect(obj){
}
	     
function innerText(node) { 
    // @author Dethe Elza 
    // @article "XML Matters: Beyond the DOM. Tips and tricks for a friendlier DOM" 
    if (node.nodeType == 3 || node.nodeType == 4) { 
	return node.data; 
    } 
    var i; var returnValue = []; 
    for (i = 0; i < node.childNodes.length; i++) { 
	returnValue.push(innerText(node.childNodes[i])); 
    } 
    return returnValue.join(''); 
}
//function onButtonAll()
//{
	if(document.captureEvents)
	    document.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.MOUSEOUT);
	document.onmousedown = document.onmouseup = document.onmouseout = function(e)
	{
	    if (!e)
	    {
		e = window.event;
		elm = e.srcElement;
	    }else
		elm = e.target;
	    if (elm.tagName=="IMG" && elm.getAttribute('pbs')=='1')
	    {
		if (e.type=="mousedown")
		    onButtonDown(elm);
		else
		    onButtonUp(elm);
	    }
	}
	
//}
				
$(function(){
//    alert(window.location.hash)
    $("#content .tableItem tr").each(function(){
	if ($(this).find('td').length>1){
	    var $h1 = $(this).find("td:first .topicItem");
	    var $h2 = $(this).find("td:last .topicItem");
	    var pad;
	    if ($h2.height()>$h1.height()){
		pad = ($h2.height() - $h1.height()) /2;
		$h1.css({height: $h2.height()+'px'})
	    }else{
		pad = ($h1.height() - $h2.height()) /2;
		$h2.css({height: $h1.height()+'px'})
	    }
	}
    });
//	кнопка справа, показывает где корзина
    $("body").append('<div id="gotobasket" title="Перейти к корзине">Корзина</div>');
    var sbasket =  $("#shortbasket");
    var sh = sbasket.offset().top + sbasket.height() - 10;
    var defCoordSM;
    var CoordSMenable = false;
    var tmpie = ($.browser.msie && $.browser.version=="6.0")? true : false;
    if ($("#content table").is(".smalllink")){
	$.get("/Service/getLinkOnChildDir.php",{ids:"30,31"},function(html){
	    $("body").append(html);
	    $(".bredcump div").css({width:$("#content").width()/4 - 1});
	    $(".bredcump").css({width: $("#content").width(), left: $("#content").offset().left});
	});
	defCoordSM = {offset: $("#content .smalllink").offset(), width: $("#content").width(),bottom: $("#content .smalllink").offset().top + $("#content .smalllink").height()}
/*
	$(".smalllink a","#content").each(function(e){
	    defCoordSM[e] = { width: $(this).width(), offset: $(this).offset(), dtop: ($(this).offset().top - Math.floor(e / 2) * 31)}
	})
*/
	CoordSMenable = true;
    }
    
    var tmptop;

    $(window).scroll(function(){
	if($("#shortbasket").find('.buttonItem').is('.buttonItem')){
	    if ($(this).scrollTop() > sh)
		$("#gotobasket").show();
	    else
		$("#gotobasket").hide();
	    $("#gotobasket").css({
		top:$(this).scrollTop()
	    });
	}else{
	    $("#gotobasket").hide();
	}
	tmptop = $(window).scrollTop();
	if (CoordSMenable){
	    if ($(window).scrollTop() > defCoordSM.bottom ){
		$(".bredcump").show()
		if (tmpie)
		    $(".bredcump").show().css({top: $(window).scrollTop(), position: "absolute"});
	    }else{
		$(".bredcump").hide();
	    }
	}
/*
	if (CoordSMenable){
	    $(".smalllink a","#content").each(function(e){
		defCoordSM[e].tmptop =  tmptop + Math.floor(e / 2) * 30;
		if ($(window).scrollTop() > defCoordSM[e].dtop )
		    if (tmpie){
			$(this).css({"top": $(window).scrollTop() + Math.floor(e / 2) * 30, position: "absolute",width: defCoordSM[e].width})
		    }else
			$(this).css({position: "fixed",top: Math.floor(e / 2) * 30, width: defCoordSM[e].width});
		else
		    $(this).css({"top": defCoordSM[e].offset.top,position: "static"})
	    })
	}
*/
    });
    $("#gotobasket").click(function(){
	var cur = $(window).scrollTop();
	var n;
	for (i=cur; i > 0; i=i-20){
	    $(window).scrollTop(i).trigger('scroll');
	}
    });

    $("tr.chtd0, tr.chtd1").hover(function(){
	$(this).addClass("trOver");
    },
    function(){
	$(this).removeClass("trOver");
    });

})

