var isNN = (navigator.appName.indexOf("Netscape")!=-1);

$(document).ready(
	function(){
		$('input[title!=""]').hint();
		
		$('#quickSearchForm').submit(function(){
			var text = $('#searchField').val();
			if(text.length < 2){
				GetErrorMessage('A keresendő szöveg rövidebb, mint 3 karakter!');
				return false;
			}
			document.location.href= '/kereses/' + text + '/oldal/1';	
			return false;
		});
		var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
		if($.browser.mozilla){
			$('.xmasProductList').css('background-position','212px 88px');
		}
		if($.client.os == "Windows" && $.browser.mozilla){
			$('.xmasProductList').css('background-position','212px 89px');
		}
		if($.client.os == "Windows" && is_chrome){
			$('.xmasProductList').css('background-position','212px 89px');
		}
		if($.browser.msie){
			if(Math.floor(jQuery.browser.version) == 7)
				$('.xmasProductList').css('background-position','212px 91px');
			if(Math.floor(jQuery.browser.version) == 8)
				$('.xmasProductList').css('background-position','212px 89px');
		}
				
		/*$('a').each(
			function(i){
				var href = '';
				if($(this).attr('href') != undefined && $(this).attr('href').indexOf('#') != 0){
					href = $(this).attr('href');
				}
				if(href.length > 0 && $(this).attr('norewrite') != 'true' ){
					href = 'Redirect(\'' + href + '\');';
					$(this).attr('href' , 'javascript:' + href);
				}								
			}
		);*/
		
		$('#mainOffer .promos').cycle({
			fx: 'fade',
			tiemout: 9000,
			prev: '#prev',
			next: '#next'
		});
		
		$('#pause').click(function(e){
			e.preventDefault();
			if($(this).hasClass('pause')){
				$('#mainOffer .promos').cycle('pause');
				$(this).removeClass();
				$(this).addClass('resume');
			}
			else{
				$('#mainOffer .promos').cycle('resume', true);
				$(this).removeClass();
				$(this).addClass('pause');
			}
		});
		
		$('#product .images ul li').mouseover(function(){
			$(this).addClass('active');
		});
		
		$('#product .images ul li').mouseout(function(){
			$(this).removeClass('active');
		});
		
		$('#product .images ul li a').click(function(e){
			e.preventDefault();
			$('#product .images .img a').hide();
			$($(this).attr('href')).show();
		});
		
		$('#product .images .img a').lightBox({fixedNavigation:true});
		
		$('#review').delay(10000).fadeTo(500, 0, function () {
			$('#review').slideUp(500);
		});

		$('#cart a.btn').click(function(e){
			e.preventDefault();
			if(!$(this).hasClass('inactive')){
				shipmentType = $(this).attr('rel');
				$.post(
						'/@/kosar.php?shipment=1',
						{shipmentType:shipmentType},
						function(html){
							if(html == 'ok'){
								$('#summa span').hide();
								$('#price'+shipmentType).show();
								$('#cart a.btn').removeClass('active');
								$('#cart a.btn[rel='+shipmentType+']').addClass('active');
							}else{
								GetErrorMessage('Hiba!');
							}
						}
					);
			}
		});

	}
	
);

function refreshHomeSales(){
	$.ajax({
		type: "GET",
		url: "/@/refreshhomebox.php?type=sale",
		success: function(result){
			$('#homeSales').html(result);
		}
	});	
}

function refreshHomeNew(){
	$.ajax({
		type: "GET",
		url: "/@/refreshhomebox.php?type=new",
		success: function(result){
			$('#homeNew').html(result);
		}
	});	
}


function keyfilter(filter, event) {
	if (!event) event=window.event;
	var c=getCharCode(event);
	if (c && !filter(c)) cancelEvent(event);
}

function getCharCode(event) {
	return typeof(event.charCode)=="undefined" ? (event.keyCode || event.which) : event.charCode;
}

function cancelEvent(event) {
	if (!event) event=window.event;
	if (event.preventDefault) event.preventDefault();
	else event.returnValue=false;
}

function keyfilter_Digit(event) {
	keyfilter(function(c) {return (c >= 48 && c <= 57) || c==13},event);
}

function GetErrorMessage(message){
	$('#errorContent').html(message);
	var height = ($('#errorContent').height() + 30);
	if(height<48){
		height = 48;
	}
	tb_show('Butlers Webshop', '/@/showError.php?height=' + height +'&width=' + ($('#errorContent').width() + 100) + '&inlineId=WebhopDiv');	
}

function OnEnter(e, functionName){
	var keyCode = (isNN) ? e.which : e.keyCode;
	if(keyCode == 13){
		if(e.keyCode == 13) {
			if (functionName != '')
				eval(functionName);
		}
	}
}

function GetEnter(funct){
	alert(funct);
}

function collectFormData(formId){
	data = new Object();
	var formValid = true;
	$('#'+formId+' [datainput=yes]').each(function(i){
		if(this.getAttribute('validate')){
			eval(this.getAttribute('validate'));
			if(this.getAttribute('valid') != 'true'){
				formValid = false;
				if(this.getAttribute('errormessage'))alert(this.getAttribute('errormessage'));
				else alert('hiba a '+((this.getAttribute('fieldname'))?(this.getAttribute('fieldname')):(this.name))+' mezőben!');
			}
		}
		if(this.getAttribute('calculate')){
			eval(this.getAttribute('calculate'));
			data[this.id] = this.getAttribute('calculated');
		}
		else{
			if(this.type == 'checkbox'){
				if(this.checked)data[this.id] = this.value;
				else data[this.id] = this.getAttribute('notvalue');
				
			}else{
				data[this.id] = $(this).val();
			}
		}
	});
	//console.dir(data);
	if(formValid){
		return data;
	}else{
		return false;
	}
}

function Redirect(url){	
	$.post(
		'/shop/!/redirect.php?r=1',
		{url:url},
		function(html){
			if(html == 'done'){
				document.location.href = url;
			}else{
				GetErrorMessage('Sajnos oldalunk most nem működik!');
				//alert('Sajnos oldalunk most nem működik!');
			}
		}
	);
}

function getMenuItem(id){
	var LiClass = $('#li_' + id).attr('class');
	if(LiClass == 'zarva'){
		$('li.nyitva').each(
			function(){
				$(this).attr('class', 'zarva');
			}
		);
	
		$('#li_' + id).attr('class', 'nyitva');
	}else{
		$('#li_' + id).attr('class', 'zarva');
	}
	
}

function GetProductList(){
	var price = $('#select_kat').val();
	var marka = $('#select_brand').val();
	var order = $('#select_order').val();
	var menuUrl = $('#menuUrl').val();
	var page = $('#page').val();
	document.location.href="/" + menuUrl + "/oldal/" + 1 + "/ar/" + price + '/marka/' + marka + '/listrendezes/' + order;
}

function GetProductListToSearch(){
	var price = $('#select_kat').val();
	var marka = $('#select_brand').val();
	var order = $('#select_order').val();	
	var text = $('#text').val();
	var page = $('#page').val();
	document.location.href="/kereses/" + text + "/oldal/" + 1 + "/ar/" + price + '/marka/' + marka + '/listrendezes/' + order;
}

function SaveComment(){
	$.post('/!/comment.php?s=1',
		{content:$('#opinion_msg').val(), productId:$('#productId').val()},
		function(html){
			var res = html.split(';');
			if(res[0] == 'error'){
				var errors = res[1].split('===');
				for(var i=0; i < errors.length; i++){
					var temp = errors[i].split('###');
					GetError(temp[0], temp[1]);
				}
				return false;
			}else{
				$('#opinion_msg').val('');
				GetErrorMessage('Köszönjük, hogy véleményezte termékünket!');
				//alert('Köszönjük, hogy véleményezte termékünket!');
				//document.location.reload();
			}
		}
	);
}

function GetRate(pId, uId, value){
	$.post('/!/customer_rate.php?s=1',
		{productId:pId, userId:uId, rate:value},
		function(html){
			var res = html.split(';');
			if(res[0] == 'error'){
				GetErrorMessage(res[1]);
				//alert(res[1]);
			}else{
				GetErrorMessage('Köszönjük, hogy értékelte termékünket!');
				//alert('Köszönjük, hogy értékelte termékünket!');
				//document.location.reload();
			}
		}
	);
}

function Search(inputId){
	var text = $('#' + inputId).val();
	if(text.length < 2){
		GetErrorMessage('A keresendő szöveg rövidebb, mint 3 karakter!');
		//alert('A keresendő szöveg rövidebb, mint 3 karakter!');
		return false;
	}
	if(text == 'keresés'){
		return false;
	}	
	document.location.href= '/kereses/' + text + '/oldal/1';	
}

function SearchGlobal(inputId){
	var text = $('#' + inputId).val();
	if(text.length < 3){
		GetErrorMessage('A keresendő szöveg rövidebb, mint 3 karakter!');
		//alert('A keresendő szöveg rövidebb, mint 3 karakter!');
		return false;
	}
	if(text == 'keresés'){
		return false;
	}
	document.location.href= '/kereses/?szo=' + text;	
}


function SendForm(){
	HideErrors();
	if ($('#receivingType') != null)
	{
		if ($('#method1').attr('checked') == true)
			$('#receivingType').val('1');
		else
			$('#receivingType').val('0');
	}
	$.post('/!/sendorders.php?s=1',
		collectFormData('OrderForm'),
		function(html){
			var res = html.split(';');
			if(res[0] == 'error'){				
				GetErrorMessage(res[1]);
				return false;
			}else{
				document.location.href = '/megrendeles_harmadik_lepes';
			}
		}
	);
}

function GetError(obj, text){
	$('#' + obj).html(text).css('display', 'block');
}

function HideErrors(){
	$('span.error').each(
		function(){
			$(this).html('').css('display', 'none');
		}
	);
}

function BasketLogin(){
	$.post(
		'/!/login.php?s=1',
		{name:$('#name').val(), pw:$('#pw').val()},
		function(html){
			var res = html.split('###');
			if(res[0] == '2'){
				GetErrorMessage(res[1]);
			}else{
				//alert('Sikeres belépés!');
				window.location.reload();
			}
		}
	);
}

function Login(){
	$.post(
		'/!/login.php?s=1',
		{name:$('#name').val(), pw:$('#pw').val()},
		function(html){
			var res = html.split('###');
			if(res[0] == '2'){
				GetErrorMessage(res[1]);
			}else{
				//alert('Sikeres belépés!');
				document.location.href="/";
			}
		}
	);
}

function Logout(){
	$.post('/!/logout.php',
		function(html){
			document.location.href="/";
		}
	);
}

function SendNewPw(){
	$.post('/!/login.php?npw=1',	
		{name:$('#name').val()},
		function(html){
			var res = html.split('###');
			if(res[0] == '2'){
				GetErrorMessage(res[1]);
			}else{
				GetErrorMessage('Az új jelszavát elküldtük az e-mail címére!');
				//document.location.href="/shop/";
			}
		}
	);
}

function Registration(){
	$.post('/!/login.php?register=1',	
		{
			name:$('#name').val(), 
			nick_name:$('#nick_name').val(),
			email:$('#email').val(),
			pw:$('#pw').val(),
			pw2:$('#pw2').val(),
			privacy:$('#privacy').val(),
			gender:$('input:radio[name=gender]:checked').val()
		},
		function(html){
			var res = html.split('###');
			if(res[0] == '2'){
				GetErrorMessage(res[1]);
			}else{
				GetErrorMessage('Sikeres regisztráció, az aktiváláshoz küldtünk önnek egy e-mailt!');
				//document.location.href="/shop/";
			}
		}
	);
}

function AddToCompare(productId, inproduct, url, anchor){		
	var baseUrlArray = document.location.href.split('#');
	var baseUrl = baseUrlArray[0];
	$.post('/@/addtocompare.php?s=1',
		{productId:productId, anchor:anchor},
		function(html){
			if(inproduct){
				document.location.href = url;
				/*if(url.indexOf(baseUrl) == 0){
					document.location.reload();				
				}*/
			}else{
				document.location.href = url;
				//document.location.reload();
			}
			/*if(html == 're'){
				GetErrorMessage('A terméket eltávolítva!');				
			}else{
				GetErrorMessage('A terméket hozzáadva!');
			}*/
		}
	);
}

function FindYPosOf( obj )
{
	var Y = 0;
	if ( obj.offsetParent )
	{
		while ( obj.offsetParent )
		{
			Y += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else
	{
		Y = obj.y;
	}
	return ( Y );
}

function newsletterSubscribe(){
	$.post(
			'/!/newsletter.php?s=confirm',
			{
				email:$('#email').val(),
				listId:$('#listId').val()
			},
			function(html){
				var res = html.split('###');
				GetErrorMessage(res[1]);
			}
		);
}

function newsletterSubscribe2(){
	$.post(
			'/!/newsletter.php?s=subscribe',
			{
				email:$('#email').val(),
				listId:$('#listId').val()
			},
			function(html){
				var res = html.split('###');
				GetErrorMessage(res[1]);
			}
		);
}

function newsletterSubscribeContent(){
	$.post(
			'/!/newsletter.php?s=confirmContent',
			{
				email:$('#emailContent').val(),
				listId:$('#listIdContent').val()
			},
			function(html){
				var res = html.split('###');
				GetErrorMessage(res[1]);
			}
		);
}

function newsletterSubscribe2Content(){
	$.post(
			'/!/newsletter.php?s=subscribeContent',
			{
				email:$('#emailContent').val(),
				listId:$('#listIdContent').val()
			},
			function(html){
				var res = html.split('###');
				GetErrorMessage(res[1]);
			}
		);
}

function SendReview(){
	$.post(
		'/!/send_review.php',
		$("#reviewForm").serialize(),
		function(html){
			if(html == 'done'){
				//GetErrorMessage('Köszönjük visszajelzését!');
				document.location.href = '/visszajelzes_kuldese/koszonjuk';
			}
		}
	);
}

