function goTo (page) {

	if (page != "" ) {
		document.location.href = page;
	}
	return false;
}

function copyData( id ) {
	if ( id == "billing" ) { 
		$('#szamlaDiv').find('input').each( function() {
			if ( $(this).attr('name') != 'billing_szemelyes' ) {
				$(this).val($('[name="'+( $(this).attr('name').replace(''+id+'_', '') )+'"]').val());
			}
		});
	}
	else if ( id == "deliver" ) {
		var answer = confirm("Ugyanazok mint a számlázási adatok?"); {
			if (answer){
				$('#szallitasadatok').find('input').each( function() {
					if ( $(this).attr('name') != 'deliver_type' ) {
						$(this).val($('[name="'+( $(this).attr('name').replace(''+id+'_', 'billing_') )+'"]').val());
					}
				});
				if ( $('input[name="deliver_email"]').val() == '' && $('input[name="email"]').val() != '' ) {
					$('input[name="deliver_email"]').val($('input[name="email"]').val());
				}
				if ( $('input[name="deliver_email"]').val() == '' && $('input[name="checkemail"]').val() != '' ) {
					$('input[name="deliver_email"]').val($('input[name="checkemail"]').val()); 
				}
				if ( $('input[name="deliver_email"]').val() == '' && $('input[name="ownemail"]').val() != '' ) {
					$('input[name="deliver_email"]').val($('input[name="ownemail"]').val());
				}
			}
		}
	}
}

function addMainMessage(text, error) {
	classN = 'errorSucc';
	if (error) classN = 'errorMess';
	mBox = $("#messageBox");
	if ( mBox.css('display') == 'none' ) {
		offLeft = $(".quickCartP").offset().left;
		$("#messageBox p").remove();
		$("#messageBox span:first").before("<p>"+text+"</p>");
		mBox.fadeIn("fast").delay(3000).fadeOut("fast");
		}
}

function setErrorLogs(obj) {
	$('.comm').html('');
	if ( obj.rettype == 'error' ) {
		if (obj.params) {
			for( i in obj.params ) { $('[name="'+i+'"]').parent().next('.comm').html(obj.params[i]); }
		} else if (obj.message) {
			addMainMessage(obj.message, 1);
		}
	} else if ( obj.rettype == 'succ' ) {
		if (obj.page) { goTo(obj.page); }
		else if (obj.message) {
			addMainMessage(obj.message, 0);
		}
	}
}

function setLoginText(obj) {
	if (obj.type == 'loggdin') {
		if ( obj.text ) goTo(obj.text);
		else history.go(0);
	} else { alert(obj.text); }
}

function priceBg(className, spanClass) {
	if (!className) return false;
	if (!spanClass) spanClass = className;
	$('.'+className).change().html(function() {
		return '<span class="'+spanClass+'l"></span><span class="'+spanClass+'c">'+$(this).html()+'</span><span class="'+spanClass+'r"></span>';
	});
	$('.'+className).removeClass(className).addClass(spanClass);
}

function getGroup() { 
}

function getGroupData(sku, key) {
	sku2 = sku.replace('-','_');
	return eval('productGroupData.'+sku2+'.'+key)
}

function setGroupData(sku, key, value) {
	eval('productGroupData.'+sku+'.'+key+' = "'+value+'"')
}

var productGroupData = null;
function changeProductGroup(obj) {
	newSku = obj.val();
	$('input[name="product_sku"]').val(newSku);
	$('select[name="count"] option')[getGroupData(newSku,'cart_count')].selected ='selected';
	
	$('div.product_price').html(getGroupData(newSku,'details_pricelist'));
	priceBg('product_pricePrice', 'price');

	imageP = getGroupData(newSku,'product_image');
	$('p.product_image a').attr('href', imageP.path);
	$('p.product_image img').attr('src', imageP.thumb);
}

function transit_hide(){
	$("#dt2, #dt22").click(function(){
		$(".transitCost").hide();
		$(".szemelyesen").show();
		$(".futarral").hide();
	});

	$("#dt1, #dt11").click(function(){
		$(".transitCost").show();
		$(".szemelyesen").hide();
		$(".futarral").show();
	});
}

function ajaxform(){
		$("form.addToCart, .formClass, form.addToFav, form.delfav").live("submit", function(){
			retText = '&time='+ new Date().getTime();
			$.post(
				$(this)[0].action+retText,
				$(this).serialize(),
				function(responseText) {
					if ( responseText ) {
						resObj = eval(responseText);
						if (resObj.mod == 'cart') setCartText(resObj, resObj.html);
						if (resObj.mod == 'login') setLoginText(resObj);
						if (resObj.mod == 'fav') setFavText(resObj, resObj.html);
						//if ( $(".product_detailsDiv").length ) { getGroup(); }
					
					}
				}
			);

			return false;
	});
}

function megjelen(r){
	$('.feher, .zold').show(); 
	$('.adatlap').hide('slow');
	$('.main_'+r).hide('slow');
	$('.datas_'+r).show('slow');

}

function bezar(r){

	$('.main_'+r).show('slow');
	$('.datas_'+r).hide('slow');
	
}

function dropdown_menu() {
	$(".dropdown").each(function() {
		var theID = $(this).attr('id');
		dropRunner(theID);
	});
}

function getURLParameter(name) { //urlcímből paramétek kivétele
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}

function setCookie(c_name,value,exdays){
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getCookie(check_name){
// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function dropRunner(id) {
	$("#"+id+" dt a").click(function() {
		$("#"+id+" dd>ul").toggle();
	});

	$("#"+id+" dd ul li a").click(function() {
		var text = $(this).html();
		$("#"+id+" dt a span").html(text);
		$("#"+id+" dd>ul").hide();
	}); 

	$(document).bind('click', function(e) {
		var $clicked = $(e.target);
		if (! $clicked.parents().hasClass("dropdown"))
			$("#"+id+" dd>ul").hide();
	});
}

function idozito(portfolioi){
$('#portfolio'+portfolioi).innerfade({
		
						speed: 2000,
						timeout: 5000,
						type: 'sequence',
					//	containerheight: '142px'
					});
	portfolioi++;
	if(portfolioi < 4){
	setTimeout("idozito()",200);
	}
}

function JSAutoBoxChanger() {
	$('.JSChanger').each(function() {
		var default_value = this.value;
		$(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});
	if ( $('#jelszo_box') ) {
		$('#jelszo_box-fake').show();
		$('#jelszo_box').hide();

		$('#jelszo_box-fake').focus(function() {
			$('#jelszo_box-fake').hide();
			$('#jelszo_box').show();
			$('#jelszo_box').focus();
		});
		$('#jelszo_box').blur(function() {
			if($('#jelszo_box').val() == '') {
				$('#jelszo_box').hide();
				$('#jelszo_box-fake').show();
			}
		});
	}
}

function mouseSet() {
	$('.opacity60').hover(function(){
	$(this).fadeTo('fast', 0.33);
	});
	
	$('.opacity60').mouseleave(function(){
	$(this).fadeTo('fast', 1);
	});
	
	$('.fav_det').mouseover(function(){
	
		$('.close_span', this).show();
	});
	
	$('.fav_det').mouseleave(function(){
	
		$('.close_span').hide();
	});
}

function fancyboxSetup() {
	$("a.gomb2").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'fade',
		'speedIn'		:	600,
		'speedOut'		:	200,
		'onComplete'	:	function(){
		},
		'hideOnOverlayClick' : false,
		'hideOnContentClick': false
	});
	
	$("a.FullGallery_IMG").fancybox({
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'speedIn'		:	200,
		'speedOut'		:	200,
		'onComplete'	:	function(){
		},
		'hideOnOverlayClick' : true
//		'hideOnContentClick': true
	});
	
	$("a.productImage").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600,
		'speedOut'		:	200,
		'onComplete'	:	function(){
		},
		'hideOnOverlayClick' : true,
		'hideOnContentClick': true
	});
	
	$(".fancyboxFrameCont").fancybox();
	
	$(".fancyboxFrameCont2").fancybox({
	'scrolling'		: 'no',
	'titleShow'		: false,
	'onComplete'		: function() {
	    Ajanlatkeresurlap(); //hondimpexhez ajánlatkérés ürlapnak a termékitöltő mezője
	}
});
}

$(document).ready(function() {
	$(".iveskeret").html(function() {
		$(this).css('position', 'relative');
		return $(this).html()+'<span class="ltimg"></span><span class="rtimg"></span><span class="lbimg"></span><span class="rbimg"></span>';
	});

	priceBg('gomb','gombp');
	priceBg('gomb2','gomb2p');
	ajaxform();
//	transit_hide();

//	$(".search_input").autocomplete( '?l1=search&l2=plug_search&l3=main', { minChars: 2 });

	$(".szemelyesen").hide();

	$('.mainMessage').show("slow");

	priceBg('product_pricePrice', 'price');

	$('.groupsel').change(function() { changeProductGroup($(this)); });

	$("#center").css('min-height', $("#leftCont").css("height"));

	$('span #submitLP').click(function(){ $('lost_password').submit(); });

	fancyboxSetup(); //fancybox beállítások
	
	mouseSet(); //egérrel kapcsolatos események (hover, opacity, mouseleave, stb..);
	
	idozito(1); //képváltó script
	JSAutoBoxChanger(); //javascript alapú doboz tartalomváltó a dobozba történő bekattitáskor
	
	dropdown_menu(); //javascriptes lenyílómenü
	
//	Ajanlatkeresurlap(); //hondimpexhez ajánlatkérés ürlapnak a termékitöltő mezője
	
	unSelectable(); //azon mezők nem kijelőlhetővé tétele amik a megfelelő osztályt tartalmazzák
	FullHTMLSlideGallery(); //slide gallery
	
});

function Ajanlatkeresurlap() {
	if (document.getElementById('formID_2')) {
		$('#formID_2 .form_generator #elements0 input:[name=form_elements1]').val(getCookie('termeknev').replace("+", " "));
	}
}

function unSelectable() {
	$('.unSelectable').live().each(function() {           
        $(this).attr('unselectable', 'on')
               .css({
                   '-moz-user-select':'none',
                   '-webkit-user-select':'none',
                   'user-select':'none'
               })
               .each(function() {
                   this.onselectstart = function() { return false; };
               });
});}

function FullHTMLSlideGallery() {
	if ( $('.GallerySlide') ) { 
		var numbr = 0;
		$('.GallerySlide').each(function() {
			numc = 'controls'+numbr;
			numbr++;
			$(".GallerySlide.numbr"+numbr).easySlider({ numeric: true,numericId: numc,numericClass: 'controls' }); 
		});
	}
}
