/**
 * @autor: Zbynek Riha
 * @copyright: Neutral Solution s.r.o., http://www.neutral-solution.cz/
 * 2011
 */
var bLinkSlideShowToImg = false;

function OpenLink(link, type){
	if (window.navigator.appName == 'Microsoft Internet Explorer') {
		if(link.substr(0, 7) != 'http://'){
			link = document.getElementsByTagName('base')[0].href + link;
		}
	}
	switch(type){
		case 'popup':
			OpenPopup(link);
			break;
		case 'same':
		default:
			window.location = link;
			break;
	}
}

function OpenPopup(link){
    var wpop = window.open(link, "", "dependent=yes, menubar=no, location=no, resizable=yes, toolbar=no, screenX=50, screeny=50, scrollbars=yes" );
}

function CloseWindow(){
	window.close();
}

function MoveBgImg(elem, type){
	if (type == 'of') {
		elem.style.backgroundPosition = "top 50%";
	}else{
		elem.style.backgroundPosition = "top 0%";
	}
}

function ChangeSelect(id, inputName, na_znak){
	var selVal = document.getElementById(id).value;
	SendForm(inputName, selVal, na_znak);
}

function ClickAndVal(val, inputName, na_znak){
	SendForm(inputName, val, na_znak);
}

function CreateElemet(elname, type, name, val){
 	var i = document.createElement(elname);
	i.setAttribute('type', type);
	i.setAttribute('name', name);
	i.setAttribute('value', val);
	return i;
}

function SendForm(inputName, inputValue, na_znak){
 	var mf = document.createElement('form');
	if(document.location.toString().indexOf(na_znak) > 0){
		na_znak = "";
	}
	if(na_znak.length > 0){
		na_znak = "#" + na_znak;
	}
	mf.setAttribute('action', document.location + na_znak);
	mf.setAttribute('method', 'post');
	mf.setAttribute('name', 'f' + inputName);
 	mf.setAttribute('target','_self');
	
	mf.appendChild(CreateElemet('input', 'text', inputName, inputValue));
	mf.appendChild(CreateElemet('input', 'submit', 's' + inputName, 'from js'));
	$('body').append(mf);
	mf.submit();
}

function checkMinLength(){
	var text = $('#search_field').val();
	if(text.length > 2){
		return true;
	}
	alert('Fraze pro vyhledávání musí být delší než tři znaky');
	return false;
}

function delConfirm(){ 
	if(window.confirm("Opravdu si přejete smazat všechny položky?"))
		return true;
	else
		return false;
}

function sendOrder(form, field, val, copyForm){
	form = '#' + form;
	if (field != undefined && val != undefined) {
		$(form + " input[name='" + field + "']").val(val);
	}
	if(copyForm != undefined){
		var html = $('#' + copyForm + ' .form-table').clone().css({display: 'none'}).appendTo(form);
	}
	$(form).submit();
	return true;
}

function switchSh(form){
	$('#' + form).submit();
}

function disableDoSaleOrder(){
	$('a#do-saleorder').addClass('disabled').click(function(e){e.preventDefault();alert('Objednávku si nejprve nechte přepočítat')});
	$('form#cartForm input[name=\'recount\']').val(1);
}

function linkSlideShowToImg(selector){
	if (!bLinkSlideShowToImg) {
		$(document).ready(function(){
			var result = $(selector).has('img');
//			alert(result.length);
			result.addClass('doc-img');
			Slideshow.jQueryMatchImagesSlideshow(result);
		});
		bLinkSlideShowToImg = true;
	}
}

function initImageZoom(field){
	Slideshow.jQueryMatchImagesSlideshow($(field), 1200);
}

/**
 * 
 * @param {jQuery} jqObj
 */
function emptySwitch(jqObj){
	for(var i=0; i < jqObj.length; i++){
		jqObj.get(i).emptyValue = jqObj.get(i).value;
	}
	jqObj.focus(function(){
		var tg = $(this).get(0);
		if(tg.value == tg.emptyValue){
			tg.value = '';
			if(tg.id == 'heslo'){
				tg.type = 'password';
			}
		}
	});
	jqObj.focusout(function(){
		var tg = $(this).get(0);
		if (tg.value.length == 0) {
			tg.value = tg.emptyValue;
			tg.type = 'text';
		}
	});
}

function prodDetailMove(target){
	var pdObj = $(target);
	var pos = 0;
	var maxPos = pdObj.find('span.img').length - 3;
	var bMove = false;
	var moveStep = 95;
	var rollerObj = pdObj.find('.roler-in');
	var moveLeft = function(e){
		if((pos > 0) && !bMove){
			pos--;
			var off = rollerObj.position();
			rollerObj.animate({left: (off.left + moveStep)}, 500);
		}
		e.stopPropagation();
		return false;
	}
	var moveRight = function(e){
		if((pos < maxPos) && !bMove){
			pos++;
			var off = rollerObj.position();
			rollerObj.animate({left: (off.left - moveStep)}, 500);
		}
		e.stopPropagation();
		return false;
	}
	pdObj.find('a.move.left').click(moveLeft);
	pdObj.find('a.move.right').click(moveRight);
}

function prodDetailOrder(form_id, addItem){
	$('#' + form_id).submit(function(){
		var form = $(this);
		var subitem_id = null;
		var subitem = form.children('select[name="subitem"]');
		if(subitem.length > 0){
			subitem_id = $(subitem).val();
		}else{
			subitem_id = form.find('input[name="subitemid"]').val();
		}
		form.children('input[name="' + addItem + '"]').val(subitem_id);
		return true;
	});
}

function newsCycle(news_id){
	var _height = 180;
	var duration = 500;
	var margin = 10;
	var zeropx = '0px';
	var ncnt = $('#' + news_id);
	var news = ncnt.find('p.new span.news-name a');
	var bAnimeDurring = false;
	var last = null;
	news.mouseover(function(){
		Debuger.trace('mouse over');
		if(!bAnimeDurring && !$(this).attr('open')){
			var hideImage = $(this).parent().parent().find('span.image.hide');
			if(hideImage.length > 0){
				bAnimeDurring = true;
				var notHideImage = ncnt.find('p.new span.image:not(.hide)');
				hideImage.css({height: 0, 'margin-bottom': 0, padding: 5});
				hideImage.removeClass('hide');
				$(last).removeAttr('open');
				
				if (notHideImage != undefined) {
					notHideImage.removeAttr('open');
					notHideImage.animate({
						  height: 0
						, 'margin-bottom': 0
					}, duration, function(){
						$(this).addClass('hide').css({padding: 0});
						bAnimeDurring = false;
					});
				}
				hideImage.animate({
					  height: _height
					, 'margin-bottom': margin
				}, duration, function(){
					bAnimeDurring = false;
				});
				$(last).removeAttr('open');
			}
			last = this;
			$(this).attr('open', 1);
		}
	});
}

function redirectCountDown(redirTime){
	$('#redir-bar .redir-border span').animate({width: 0}, redirTime);
}

