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, id){
	createMessage("Минуточку..." + spinnerStr + "<br><br><span>Товар добавляется в корзину</span>", 500);
	var obj = document.getElementById('shortbasket');
	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 hideMessage(){
    if ($("#mess").length)
	$("#mess").remove();
}
function createMessage(mess_text){
    if (!$("#mess").length)
	$("body").append('<div id="mess"></div>');
    $("#mess").html(mess_text).css({top: $(window).scrollTop() + 200,left:Math.round($(window).width()/2) - Math.round($("mess").width()/2)}).show();
}

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 Pop2(obj,speed, className){
    	Pop2Hide();
	$("body").append('<div id="imageLargeFL"><img src="'+$(obj).attr("pbsrc")+'" width="100%" height="100%" onclick="Pop2Hide()"></div>');
	$(obj).addClass("abracadabraFL");
        $("#imageLargeFL").css("position","absolute");
	$("#imageLargeFL").hide();
	var pos = $(obj).position();
	$("#imageLargeFL").css("top",pos.top);
	$("#imageLargeFL").css("left",pos.left);
	var wi = getSize($(window)), im = getSize($("#imageLargeFL"));
//	alert('ww ='+wi.w + '; wh=' + wi.h + '; iw=' + im.w + '; ih=' +im.h);
//	$("#imageLargeFL").remove();
//	$(obj).removeClass("abracadabraFL");
//	return;
	var  o = GetImageDimensions(im,wi);
	$("#imageLargeFL").css("width",$(obj).width());
	$("#imageLargeFL").css("height",$(obj).height());
	$("#imageLargeFL").animate({width: o.w, height:o.h, left: o.x, top:o.y, fontSize: "3em", borderWidth: "10px", borderColor: "White",position: "absolute", display:"block"},"slow");
}
function Pop2Hide(){
	if ($("img").hasClass("abracadabraFL")){
	var obj = $(".abracadabraFL");
	var pos = obj.offset();
	$("#imageLargeFL").animate({width: obj.width(), height: obj.height(), left: pos.left, top: pos.top},"slow","swing",function(){
    	    $("#imageLargeFL").remove();
	    obj.removeClass("abracadabraFL");
	    obj.show();
	});
	setTimeout("",600);
	}
	
}
function getSize(obj){
    return {x:obj.scrollLeft(), y:obj.scrollTop(), w:obj.width(), h:obj.height()}
}
				
function GetImageDimensions(im,ws){
	var o = {};
	if (im.w>im.h){
	    if (im.w>ws.w)
	        o.w = ws.w;
	    else
	        o.w = im.w;
	    o.h = Math.round(o.w * im.h / im.w);
	    if (o.h>ws.h){
	        o.h = ws.h;
	        o.w = Math.round(o.h * im.w / im.h);
	    }
	}else if (im.h>im.w){
	    if(im.h>ws.h) o.h=ws.h; else  o.h=im.h;
	    o.w = Math.round(o.h * im.w / im.h);
	    if (o.w>ws.w){
	        o.w = ws.w;
	        o.h = Math.round(o.w * im.h / im.w);
	    }
	}else{
	    if (ws.w>ws.h){
	        if (im.w>ws.w) o.w = ws.w; else o.w = im.w;
	        o.h = o.w;
	    }else{
	        if (im.h>ws.h) o.h = ws.h; else o.h = im.h;
	        o.w=o.h;
	    }
	}
        o.x = Math.round(ws.x + ws.w/2) - Math.round(o.w/2);
        o.y = Math.round(ws.y + ws.h/2) - Math.round(o.h/2);
        return o;
}
																																
