//-----parametry a ajax--------------------
var TempAjax = "";
var TempAjaxNavBar = "";
var timeoutGiftOn = 0;

var giftViewOld = 1;
var giftViewNew = 2;



function WorkAni(oX,oY){
    objGet('Work').style.top = (oY + document.documentElement.scrollTop).toString() + "px";
	objGet('Work').style.left = (oX + 20).toString() + "px";
	objGet('Work').style.display = 'block';
	return;
}

function GiftPrice(){
	var url = '/Gift/GiftPriceAjax.asp'
	
	$('#basket').load('/Gift/GiftPriceAjax.asp', function(){
		$('#basket').children('.price').css({'text-decoration': 'blink'});
		setTimeout(function(){$('#basket').children('.price').css({'text-decoration': 'none'});}, 3000);
		} );
	
}

function GiftString(nPage){
	
	$('#giftWait'+giftViewOld).removeClass('waitOff').addClass('waitOn');
	
	objGet('giftPage').value = nPage;
	
	var strParam = '';
	var strProducer = '';
	var strPrice = '';
	var strLast = '';
	var strActual = '|';
	
	var type = objGet('giftType').value;
	var hold = objGet('giftHold').value;

	if(nPriceMinResult > nPriceMin || nPriceMaxResult < nPriceMax){strPrice = nPriceMinResult.toString().replace('.00','') + '/' + nPriceMaxResult.toString().replace('.00','')}
	var url = gstrLangLink + '/Gift/GiftAjax.asp?type='+type+'&price='+strPrice+'&page='+nPage;
	
	window.location='#type='+type+'&price='+strPrice+'&page='+nPage+'&hold='+hold;	
	//window.location.href = url;
	//window.frames['WinStat'].location.replace('/Gift/GiftSession.asp?type='+type+'&price='+strPrice+'&page='+nPage);
	$.ajax({ url: url, async: true , success: function(data){
		var dataArr = data.split('$$$$$$$$$$');
				
		InitGiftList(dataArr[0], giftViewNew);
		InitPrice(dataArr[1]);
				
		$('#giftWait'+giftViewOld).addClass('waitOff');
		$('#giftWait'+giftViewOld).removeClass('waitOn');	
		
		$("#CommodityGiftListView"+giftViewOld).fadeOut("slow");
		$("#CommodityGiftListView"+giftViewNew).fadeIn("slow");
	
		var giftViewTmp = giftViewOld;
		giftViewOld = giftViewNew;
		giftViewNew = giftViewTmp;
		}
	}).responseText;
}


function opacityGift(id, opacStart, opacEnd, millisec, bHidden) {
	timeoutGiftOn = 1;
	
    //speed for each frame 
    var speed = Math.round(millisec / (100/5)); 
    var timer = 0;

    

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i=i-5) { 
            setTimeout("changeOpacGift(" + i + ",'" + id + "', " + bHidden +" )",(timer * speed)); 
            timer++;
        }
        
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i=i+5) 
            { 
            setTimeout("changeOpacGift(" + i + ",'" + id + "', " + bHidden +" )",(timer * speed)); 
            timer++;
        }
    }
}


//change the opacity for different browsers 
function changeOpacGift(opacity, id, bHidden) {
    var object = objGet(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
    
    if (eval(opacity) > 0 && bHidden == 1) {
        objGet(id).style.display = 'block';
        //objGet('hintBody').style.display = 'block';
    }
    if (eval(opacity) == 0 && bHidden == 1) {
        objGet(id).style.display = 'none';
        //objGet('hintBody').style.display = 'none';
    }
}


function GiftHold(sCode){
	if(objGet('giftHold').value.indexOf(sCode) == -1){
		objGet('giftHold').value = sCode+'|'+objGet('giftHold').value ;
		
		var strPrice = '';
		var type = objGet('giftType').value;
		var hold = objGet('giftHold').value;
		var page = objGet('giftPage').value;
	
		if(nPriceMinResult > nPriceMin || nPriceMaxResult < nPriceMax){strPrice = nPriceMinResult.toString().replace('.00','') + '/' + nPriceMaxResult.toString().replace('.00','')}
		
		window.location='#type='+type+'&price='+strPrice+'&page='+page+'&hold='+hold;
		
		GetHoldGift(0);
	}	
}

function GiftHoldDel(sCode){
	if(objGet('giftHold').value.indexOf(sCode) != -1){
		objGet('giftHold').value = objGet('giftHold').value.replace(sCode+'|', '');
		
		var strPrice = '';
		var type = objGet('giftType').value;
		var hold = objGet('giftHold').value;
		var page = objGet('giftPage').value;
	
		if(nPriceMinResult > nPriceMin || nPriceMaxResult < nPriceMax){strPrice = nPriceMinResult.toString().replace('.00','') + '/' + nPriceMaxResult.toString().replace('.00','')}
		
		window.location='#type='+type+'&price='+strPrice+'&page='+page+'&hold='+hold;
		
		GetHoldGift(0);
	}	
}

function GetHoldGift(nPage){
	var url = gstrLangLink + '/Gift/GiftHoldAjax.asp?clst='+objGet('giftHold').value + '&nPage=' + nPage;	
	var data = GetHttpRequest(url);
	//objGet('CommodityGiftHoldList').innerHTML = "<div><img src='/Img/ajax-loader-big.gif' alt='Waiting' /></div>";
	
	//window.location.href = url;
	
	$("#CommodityGiftHoldList").fadeTo("fast",0, function(){
		InitHoldList(data);
		$("#CommodityGiftHoldList").fadeTo("slow",1);
		}	
		 );
	
//    setTimeout("ActionHttpRequestName('" + url + "', 'CommodityGiftHoldList')", 200);
}

function NextShow(){
	if(objGet('paramHide').style.display == 'none'){
		objGet('paramHide').style.display = 'block';
		SaveCookies('wizznext', 'block');
	}else{
		objGet('paramHide').style.display = 'none';
		SaveCookies('wizznext', 'none');
	}
}
/// Drag&Drop System , pouzito z Interval.cz

	var dragobj;
	
	function m_down(event, object)
	{
	
		
		dragobj = object;      
		
		dragobj.style.zIndex = "100";	
		if(!dragobj.style.left) dragobj.style.left = "0px";
		if(!dragobj.style.top) dragobj.style.top = "0px";
			
		// Zapamatuj pozici mysi pri klintuti
		dragobj.clickX = event.clientX;  
		dragobj.clickY = event.clientY;
			
		// Zapamatuj pozici objektu pri klintuti
		dragobj.left = parseInt(dragobj.style.left); 
		dragobj.top = parseInt(dragobj.style.top);


	 	// Zacni sledovat pohyb mysi
    if(IE)
		{	
			document.attachEvent('onmousemove', m_move); 
			document.attachEvent('onmouseup', m_up); 		
		}else{
			document.addEventListener('mousemove', m_move, true);
			document.addEventListener('mouseup', m_up, true); 		
		}

return stopEvent(event);
}


function m_move(event)
{
	// Spocti rozdil mezi puvodni pozici mysi pri stisku a nynejsi pozici a zavolej proceduru objektu pro presun 
	dragobj.ondragdrop(event.clientX-dragobj.clickX, event);
	return stopEvent(event);
}

function m_up(event)
{
	// Ukonci sledovani mysi
	if(IE)
	{
		document.detachEvent('onmousemove', m_move); 	
		document.detachEvent('onmouseup', m_up); 
		if(dragobj.ondragend)dragobj.ondragend(event);
	}else{
		document.removeEventListener('mousemove', m_move, true); 
		document.removeEventListener('mouseup', m_up, true); 
		if(dragobj.ondragend)dragobj.ondragend(event);
	}
	return stopEvent(event);	
}


// Prirad objektu moznost presunu
function startDrag(what)
{
	if(what.style.position!="relative" && what.style.position!="absolute") {what.style.position="relative";}
	what.onmousedown = function(e) {if(e) {event=e;} m_down(event,what);}
	what.drag = true;
}

// Odeber objektu moznost presunu 
function stopDrag(what)
{
	what.onmousedown = null;
	what.drag = 0;
}

// Zastav sireni udalosti
function stopEvent(event)
{
	if(IE) {
		event.cancelBubble = true; return false;
	}else{
		event.stopPropagation(); event.preventDefault(); return false;
	}
}


function FormatCurr(strCurr,intRound){
	var strCurrTemp = strCurr.toString();
	strCurrTemp = strCurrTemp.toString().replace(',-','');
	strCurrTemp = strCurrTemp.toString().replace(' ','');
	strCurrTemp = strCurrTemp.replace(',','.');
	if (intRound == 1) {
	    if (euro) { var intCurr = Math.round(parseFloat(strCurrTemp) / 10) * 10; }
	    else { var intCurr = Math.round(parseFloat(strCurrTemp) / 200) * 200; }
	    
	    if(intCurr < nPriceMin){ intCurr = nPriceMin; }
		if(intCurr > nPriceMax){ intCurr = nPriceMax; }
		   
	}else{
		var intCurr = Math.round(parseFloat(strCurrTemp));
	}
	
	strCurrTemp = intCurr.toString() + ''
	
	return strCurrTemp;
}

function BackAction(){
	var url = window.location.toString();
	
	if(url.indexOf('#') > 0){
		url = gstrLangLink + '/Gift/GiftAjax.asp?'+url.substring(url.indexOf('#')+1);
	
		var strSub = url.substring(url.indexOf('price'))
		strSub = strSub.substring(strSub.indexOf('=')+1,strSub.indexOf('&'))
	
		if(strSub.indexOf('/') > 0){
			var arrData = strSub.split('/')
			
			nPriceMinResult = arrData[0];
			nPriceMaxResult = arrData[1];
			
			if(arrData[1] > 0 ){
				objGet('draggerMax').style.left = Math.round(nPriceMaxResult / nItem) + 'px';
				objGet('TextMax').innerHTML = 'do: ' + FormatCurr(arrData[1]/fCoef,0) + ' ';
			}
			if(arrData[0] > 0 ){
				objGet('draggerMin').style.left = Math.round(nPriceMinResult / nItem) + 'px';
				objGet('TextMin').innerHTML = 'od: ' + FormatCurr(arrData[0]/fCoef,0) + ' ';
			}
		}
		
		strSub = url.substring(url.indexOf('type'))
		strSub = strSub.substring(strSub.indexOf('=')+1,strSub.indexOf('&'))
		
		if(strSub.length > 0 && strSub > 0){
			objGet('giftType').value=strSub;
			activateGiftType('aMenuGiftImg'+strSub);
		}
		
		strSub = url.substring(url.indexOf('page'))
		strSub = strSub.substring(strSub.indexOf('=')+1,strSub.indexOf('&'))
		
		if(strSub.length > 0 && strSub > 0){
			objGet('giftPage').value=strSub;
		}
		
		strSub = url.substring(url.indexOf('hold'))
		strSub = strSub.substring(strSub.indexOf('=')+1,strSub.length)
		
		
		if(strSub.length > 0){
			objGet('giftHold').value = strSub;
		}
		
		if(url != ''){
			//
			var data = GetHttpRequest(url);
			var dataArr = data.split('$$$$$$$$$$');
			
			InitGiftList(dataArr[0], giftViewOld);
			InitPrice(dataArr[1]);
			
			var urlHold = gstrLangLink + '/Gift/GiftHoldAjax.asp?clst='+objGet('giftHold').value + '&nPage=1';
			data = GetHttpRequest(urlHold);
			
			InitHoldList(data);	
			
			
		}
	}
}


function InitGiftList(sGiftList, nGiftListView){
			objGet('CommodityGiftListView'+nGiftListView).innerHTML = sGiftList;
			objGet('CommodityGiftListView'+nGiftListView).innerHTML += "<div id='giftWait"+nGiftListView+"' class='waitOff' ><img src='/Img/ajax-loader-big.gif' alt='Waiting' /></div>";
}

function InitHoldList(sHoldList){
	    objGet('CommodityGiftHoldList').innerHTML = sHoldList;
}

function InitPrice(sPrice){
			var arrPrice = sPrice.split('/')
			
			nPriceMin = parseInt(FormatCurr(arrPrice[0],0));
			nPriceMax = parseInt(FormatCurr(arrPrice[1],0));
			nItem = (nPriceMax - nPriceMin)/500;
			
				if(ActualMin == parseInt(objGet('draggerMin').style.left)){
						objGet('TextMin').innerHTML = 'od: ' + formatPrice(FormatCurr(nPriceMin/fCoef,0)) + ',- ';
						nPriceMinResult = FormatCurr(nPriceMin,0);
					}else{
						if(nPriceMinResult < nPriceMin){
							objGet('TextMin').innerHTML = 'od: ' + formatPrice(FormatCurr(nPriceMin/fCoef,0)) + ',- ';
							nPriceMinResult = FormatCurr(nPriceMin,0);
							objGet('draggerMin').style.left = ActualMin + 'px';
						}else{
							objGet('draggerMin').style.left = Math.round(nPriceMinResult / nItem) + 'px';
						}
					}

			
			

				if(ActualMax == parseInt(objGet('draggerMax').style.left)){
					objGet('TextMax').innerHTML = 'do: ' + formatPrice(FormatCurr(nPriceMax/fCoef,0)) + ',- ';
					nPriceMaxResult = FormatCurr(nPriceMax,0);
				}else{
					if(nPriceMaxResult > nPriceMax){
						objGet('TextMax').innerHTML = 'do: ' + formatPrice(FormatCurr(nPriceMax/fCoef,0)) + ',- ';
						nPriceMaxResult = FormatCurr(nPriceMax,0);
						objGet('draggerMax').style.left = ActualMax + 'px';
					}else{
						objGet('draggerMax').style.left = Math.round(nPriceMaxResult / nItem) + 'px';
					}									
				}
				
				if(parseInt(nPriceMaxResult) < parseInt(nPriceMinResult)){
							objGet('TextMin').innerHTML = 'od: ' + formatPrice(FormatCurr(nPriceMaxResult - 4/fCoef,0)) + ',- ';
							nPriceMinResult = FormatCurr(nPriceMaxResult - 4,0);
							objGet('draggerMin').style.left = Math.round((nPriceMaxResult - 4) / nItem) + 'px';
				}

	
			startDrag(objGet('draggerMin'),1);
			startDrag(objGet('draggerMax'),1);
}

function ActionHttpRequestName(url, objName){
	if(url == ''){return}
	if (window.ActiveXObject){
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		httpRequest = new XMLHttpRequest();
	}

	httpRequest.open("GET", url, false);
	httpRequest.setRequestHeader("Content-Type", "text/html; charset=utf-8");
	httpRequest.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
	httpRequest.send(null);

	if (httpRequest.readyState == 4){
		if(httpRequest.status == 200){
			var data;

			data = httpRequest.responseText;
			objGet(objName).innerHTML = data;
			opacityGift('CommodityGiftHoldList', 0, 100, 200, 0);	

		}else{
			alert("Chyba pri nacitani stanky"+ httpRequest.status +":"+ httpRequest.statusText);
		}
	}
}

function GetHttpRequest(url){
	var data = '';
	
	if(url == ''){return}
	if (window.ActiveXObject){
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		httpRequest = new XMLHttpRequest();
	}

	httpRequest.open("GET", url, false);
	httpRequest.setRequestHeader("Content-Type", "text/html; charset=utf-8");
	httpRequest.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
	httpRequest.send(null);

	if (httpRequest.readyState == 4){
		if(httpRequest.status == 200){

			data = httpRequest.responseText;

		}else{
			alert("Chyba pri nacitani stanky"+ httpRequest.status +":"+ httpRequest.statusText);
		}
	}
	
	return data;
}


function processRequest(){
  if (httpRequest.readyState == 4){
    if(httpRequest.status == 200){
    	var arrData;
    	var boolShow = 0;
    	data = httpRequest.responseText;
	    objGet('BaseContentPage').innerHTML = data;
	    startDrag(objGet('draggerMin'),1);
	    startDrag(objGet('draggerMax'),1);
    }else{
        alert("Chyba pri nacitani stanky"+ httpRequest.status +":"+ httpRequest.statusText);
    }
  }
}

function activateGiftType(e){
	objGet('aMenuGiftImg1').src = objGet('aMenuGiftImg1').src.replace('_green.gif','.gif');
	objGet('aMenuGiftImg2').src = objGet('aMenuGiftImg2').src.replace('_green.gif','.gif');
	objGet('aMenuGiftImg3').src = objGet('aMenuGiftImg3').src.replace('_green.gif','.gif');
	objGet('aMenuGiftImg4').src = objGet('aMenuGiftImg4').src.replace('_green.gif','.gif');
	objGet('aMenuGiftImg5').src = objGet('aMenuGiftImg5').src.replace('_green.gif','.gif');
	
	objGet(e).src = objGet(e).src.replace('.gif','_green.gif');
}

function formatPrice(strPrice){
	var sPrice = strPrice;
	
	if(sPrice.length > 3){
		sPrice = sPrice.substring(0, sPrice.length - 3 ) + ' ' + sPrice.substring(sPrice.length - 3, sPrice.length)
	}
	
	return sPrice;
}

function jqueryFlyToCart(sId){
	
	var sImage = 'http://img.alza.cz/Foto/f6/'
	var sCode = '';
	
	if(sId.indexOf('buy') >= 0){
		sCode = sId.substring(3, sId.length);
	}else{
		sCode = sId.substring(4, sId.length);
	}
	
	sImage += sCode.substring(0, 2) + '/' + sCode + '.jpg';

	var pos = $("#"+sId).offset();
	var basketPos = $('#basket').offset();
	var flyImg = $('<img src="'+ sImage +'" alt="Test Image" title="Test Image" />');
	$('#MainTotal').append(flyImg);
	flyImg.css({position: 'absolute',
		top:pos.top, left:pos.left, height:'120px'
	}
	);
	flyImg.animate({'left':basketPos.left, 'top':basketPos.top, height:'20px', opacity:'0'}, 1000, 'linear', function()
	{ 
		flyImg.remove();
		if(objGet('gift'+sId).action.length > 0){
			objGet('gift'+sId).submit();
			objGet('gift'+sId).action = '';
		}
	} );
}