var reloadDisabled = false;
var loaded=false;
var alternateLocation = '';
var titlevariable=document.title;
var loginFocus =false;
var subscribeFocus =false;

function popUpImageKill()
	{
		if (typeof(subwin)=='undefined')
			return;
		if (typeof(subwin.closed)=='undefined')
			return;
		if (subwin.closed==true)
			return;
		subwin.close();
	}

popUpFlyer = function(e)
	{
		popUpImageKill();
		href = e.target.href;
		
		var width = flyerW;
		var height = flyerH;
		var title= 'Flyer';
		
		swleft = Math.round((self.screen.width - width)/2);
    	swtop  = Math.round((self.screen.height - height)/3);
		
		subwin = window.open("","","height="+height+",width="+width+", left = "+swleft+", top = "+swtop+", toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no");
		if (!subwin)
		{
			return false;
		}
     	subwin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml">');
     	subwin.document.write('<head><title>'+title+'</title><style type="text/css">body{margin:0;padding:0;background:#F9F9F9 no-repeat center;}</style></head>\n');
     	subwin.document.write('<body><p><img width="'+width+'"height="'+height+'" src="'+href+'" alt="'+title+'" title="" onclick="window.close();" /></p></body>\n');
     	subwin.document.write('</html>\n');
     	subwin.document.close();
		subwin.focus();
		e.preventDefault();
		e.stopPropagation();
		return false;
	}

function thisMovie(movieName) {
        if (navigator.appName.indexOf ("Microsoft") !=-1) {
            return window[movieName];
        } else {
           return document[movieName];
        }
}


function navigateId(id){
	$('#'+id).click();
}


function switchLang(){
	document.location = alternateLocation;
}

function centerContent() {
	bodyWidth = document.getElementById("body").offsetWidth;
	pageWidth = document.getElementById("page").offsetWidth;
	leftOffset = ( Math.floor(((bodyWidth - pageWidth) / 2) / 6) * 6 ) + 2;
	leftOffset = leftOffset < 0 ? 0 : leftOffset;
	document.getElementById("page").style.left = leftOffset+"px";
}

autoMoveEvent = function(){
	nextStep = moveEvent(1);
	setTimeout(autoMoveEvent,nextStep*1000);
	
}

moveEventLeft = function(event){
	event.preventDefault();
	moveEvent(-1);
	event.stopPropagation();	
	event.preventDefault();	
	return false;
}

moveEventRight = function(event){
	event.preventDefault();
	moveEvent(1);
	event.stopPropagation();	
	event.preventDefault();	
	return false;
}

moveEvent = function(offset){
	if(typeof(eventy)=='undefined'){
		return false;
	}
	if(eventy.length < 2){
		return false;
	}
	
	currentEvent = currentEvent + offset;
	if(eventy[currentEvent] == null){
		if(currentEvent < 0){
			currentEvent=eventy.length-1;
		}else{
			currentEvent = 0;
		}
	}

	
	$('#centerEvent').attr('href', eventy[currentEvent].link);


	
	$('#centerEvent img').fadeOut("slow",
		function(){
			$('#centerEvent img').attr('src', eventy[currentEvent].icon);
			$(this).fadeIn('slow');
		}
	);
	
	return eventy[currentEvent].icoSeconds;
}

assignReloadCon = function(){
	$('#mco a').not(".nofol").bind('click', reloadCon);
	$('#flyer').click(popUpFlyer);
	document.title = titlevariable;
	$('.shop-items form').unbind('submit');
	$('.shop-items form').submit(addToBasket);
	$('.shop-item').each(function(){$(this).find('a.lightbox').lightBox()});
	$('a.targetblank').unbind('click');
	$('a.targetblank').click(function(){
        window.open($(this).attr('href'));
        return false;
    });
	refreshBasket();
}
assignReloadNews = function(){
	$('a', $('#snid')).not(".nofol").not('.snl').click(reloadCon);
	$('a.snl').click(reloadNews);
}
assignReloadPoll = function(){
	$('.pollitem').bind('click', reloadPoll);
}

reloadCon = function(evt){
	if(!loaded){
		return false;
	}
	
	var href = $(evt.target).attr('href');
	if(!href){
		href = $(evt.target).parent().attr('href');
	}
	href=href.replace(new RegExp(base_web_dir.replace(/\/$/,'')),'');
	reloadFlash(href);
	evt.preventDefault();
	evt.stopPropagation();
	return false;
}

reloadFlash = function(href){
	if(base = href.match(/(http:\/\/[^\/]+)/)){
		loc = document.location+'';
		locbase = loc.match(/(http:\/\/[^\/]+)/);
		
		if(base[1] != '' && base[1] != locbase[1]){
			document.location = href;
			return;
		}
	}

	anchor = href.replace(/http:\/\/[^\/]+/,'');
	$.historyLoad(anchor);
	
}

reloadNews = function(evt){
	
	var href = $(evt.target).attr('href');
	$(".shortnews").load(href,{'ajax':1},assignReloadNews);
	evt.preventDefault();
	evt.stopPropagation();
	return false;
}
reloadPoll = function(evt){
	var href = $(evt.target).attr('href');
	$("#pollDiv").load(href,{'ajax':1},assignReloadPoll);
	evt.preventDefault();
	evt.stopPropagation();
}

callingHistory = function(hash){
	if(reloadDisabled){
		return;
	}
	loc = document.location.toString();
	afterLang = loc.match(/(\/cs\/|\/en\/)([^#]*)/);
	if(!hash){
		hash=loc;
	}
	if(hash){
		var section=hash.match(/(\/cs\/|\/en\/)([^#\?\/]*)/)[2];
		if(section!='shop'){
			$('.rightColumn,.shortnews,#pollDiv,.partners').css('display','block');
		}else{
			$('.rightColumn,.shortnews,#pollDiv,.partners').css('display','none');
		}
		$("#mco").load(base_web_dir.replace(/\/$/,'')+hash,{'ajax':1},assignReloadCon);
	}else{
		newLoc = afterLang[1]+afterLang[2];
		$("#mco").load(base_web_dir.replace(/\/$/,'')+newLoc,{'ajax':1},assignReloadCon);
	}
	reloadDisabled = true;
	setTimeout('reloadDisabled = false;',500);
}

var path='http://'+window.location.host+base_web_dir+lang+'/shop/';
var refreshBasket=function(){
	$('.basket-content input.novis').attr({disabled:'disabled'});
	$('.basket-content input[type^="text"]').change(function(){
		var variantId=$(this).parent().parent().find('input[type^="hidden"]').val();
		$.get(path+'ajax/',{variantId:variantId,quantity:$(this).val()},function(inner){$('.basket').html(inner);refreshBasket();});
	});
	$('td.deleteItem a').click(function(){
		var variantId=$(this).parent().parent().find('input[type^="hidden"]').val();
		$.get(path+'ajax/',{del:variantId},function(inner){$('.basket').html(inner);refreshBasket();});
		return false;
	});
	
	var total=$('#inShop-basket .totalCount span').html(),
	butn=$('.order-form form');
	if(total>0){
		butn.unbind('submit');
	}else{
		butn.submit(function(){
			return false;
		});
	}
}

addToBasket=function(){
	var itemId=$(this).find('.itemId').val();
	var count=$(this).find('.quantity input').val();
	var itemSize=$(this).find('.size select').val();
	$.get(path+'stock/',{id:itemId,quantity:count,size:itemSize},function(inner){
		if(inner==1){
			$.get(path+'ajax/',{id:itemId,quantity:count,size:itemSize},function(inner){$('.basket').html(inner);refreshBasket();});
		}else{
			if(lang=='cs'){
				alert('Na skladě už bohužel není dostatečné množství.');
			}else{
				alert('Unfortunately, there are not sufficient quantities in stock.');
			}
		};
	});
	return false;
} 



init = function(){
	$('.shop-items form').submit(addToBasket);


	centerContent();
	$.historyInit(callingHistory);
	$.ajaxSetup(
		{type:'GET'}
	);
	
	
	$(window).resize(function(){centerContent();});
	$('a').not(".nofol").click(reloadCon);
	$('a.snl', $('#snid')).click(reloadNews);
	
	$('a.pollitem').click(reloadPoll);
	
	/*JAK TO BEZ TOHO MOHLO FUNGOVAT, EH ???? ----->*/ assignReloadNews();

	$('#leftEvent').click(moveEventLeft);
	$('#rightEvent').click(moveEventRight);

	
	
	
	
	$(".subscribe input").hover(function(){
  		$(this).css('background', 'silver');
	},function(){
		if($(this).val() == '' && subscribeFocus == false){
  			$(this).css('background','#1f1f1f');
		}
	}).focus(function(){
  		$(this).css('background', 'silver');
  		subscribeFocus = true;
	}).blur(function(){
		subscribeFocus = false;
		if($(this).val() == ''){
  			$(this).css('background','#1f1f1f');
		}
	});

	$(".login input").hover(function(){
  		$(this).css('background', 'silver');
	},function(){
		if($(this).val() == '' && loginFocus == false){
  			$(this).css('background','#1f1f1f');
		}
	}).focus(function(){
		loginFocus = true;
  		$(this).css('background', 'silver');
	}).blur(function(){
		loginFocus = false;
		if($(this).val() == ''){
  			$(this).css('background','#1f1f1f');
		}
	});


	
	setTimeout(autoMoveEvent,7000);
	loaded = true;
	so.write("menuflashholder");
	
	loc = document.location+'';
	if(loc.match(/\?ordered=1\/?$/)){
		$('.rightColumn,.shortnews,#pollDiv,.partners').css('display','none');
	};

	refreshBasket();
	$('#conditions').click(function(){
        window.open($(this).attr('href'));
        return false;
    });
}

/*slowdownInit = function(){
  setTimeout('init()',2000);	
}*/


$(window.document).ready(init);