$(document).ready(function() {
	if ($(window).width()>1024) {
		$('#searchdiv').css('left',(parseInt($(window).width())-1024) / 2 + 12);
		$('#searchdiv').css('z-index',50);
	}
	$(window).resize(function() {
		if ($(window).width()>1024) {
				$('#searchdiv').css('left',(parseInt($(window).width())-1024) / 2 + 12);
				$('#searchdiv').css('z-index',50);
		}
	});
	/*
	$("#ujdonsag_selector").change(function () {
		//alert($('#ujdonsag_selector option:selected').val());
		turnpage('ujdonsag','',$('#ujdonsag_selector option:selected').val());
	});*/


	
	//----------------- Mouseover / mouseenter ------------------
	$("#searchinput").bind({
	  click: function(){
		if ($('#searchinput').val().length >= 3 && $('#searchinput').val()!='Keresett parfüm neve') {
			$('#searchdiv').show();
		}
	  },
	  mouseenter: function(){
		if ($('#searchinput').val().length >= 3 && $('#searchinput').val()!='Keresett parfüm neve') {
			$('#searchdiv').show();
		}
	  },
	  mouseleave: function(){
		$('#searchdiv').mouseenter(function () {
			$('#searchdiv').show();
		});
		$('#searchdiv').mouseleave(function () {
			$('#searchdiv').hide();
		});
	  }
	});
	$('#searchdiv').mouseleave(function () {
		$('#searchdiv').hide();
	});
	
	//----------------- KEYPRESS ------------------
	$('#searchinput').keypress(function(event) {
		if (event.keyCode == 38) {  //felfelé
			
		}
		if (event.keyCode == 40) {   //lefelé
			if ($('#searchdiv ul li a')) {
				
			}
		}
	});
	
	//----------------- SELECT change ------------------
	$('#celkoz').change(function() {
		//if ($('#searchinput').val().length >= 3 && $('#searchinput').val()!='Keresendő kifejezés') {
			search($('#searchinput').val(),false);
		//}
	});
	$('#illat').change(function() {
		//if ($('#searchinput').val().length >= 3 && $('#searchinput').val()!='Keresendő kifejezés') {
			search($('#searchinput').val(),false);
		//}
	});
	
	//---------------- SCROLL BAR -----------------
	//$('.scroll-pane').jScrollPane();
	
	
	show_box();
	
	
	//------------------------- TERMEK.php -> felurgó box a szállításnál -----------
	$('#szallitas_hidden').hide();
           
   if ($(window).width()>1024) {
       $('#szallitas_hidden').css('right',((parseInt($(window).width())-1024) / 2)+30);
       $('#szallitas_hidden').css('z-index',50);
   }
   $(window).resize(function() {
       if ($(window).width()>1024) {
               $('#szallitas_hidden').css('right',((parseInt($(window).width())-1024) / 2)+30);
               $('#szallitas_hidden').css('z-index',50);
       }
   });
   
   $('#szallitas_flow').mouseenter(function(){
       $('#szallitas_hidden').show();
   }).mouseleave(function(){
       $('#szallitas_hidden').hide();
   });
	
	
});







//----------------- KERESÉS -------------------

var req = false;
var cel = '';
var illat = '';
function search(inputString,show) {

	if (inputString.length >= 3 && inputString!='Keresett parfüm neve') {
		if (req) req.abort();
		
		//cel =  $("#celkoz option:selected").val();
		//illat =  $("#illat option:selected").val();
		//alert(inputString+"---"+show);
		req = $.ajax({
			type: "GET",
			url: "http://www.parfum.hu/search.php?searchstring="+inputString,
			datatype: "html",
			success: function(data){
				//alert(inputString+"-2--"+show);
				if (show == true) {
					$('#searchdiv').show();
					$('#searchdiv').css('z-index',50);
				}
				$('#searchdiv').html(data);
				show_box();
			}
		});
		
	}
	else {
		$('#searchdiv').hide();
	}
}


//----------------- turnpage -------------------

function turnpage(where,what,limit) {
	$('#'+where+'_termekek').html('<p style="text-align: center; padding: 200px 0 0 0;"><img src="images/ajax-loader.gif" /></p>');
	if (where == 'akcios') {
		limit= $('#akcios_selector option:selected').val();
		magassag =( $('#akcios_termekek').offset().top - 44);
		$('html, body').animate({scrollTop: magassag }, 500);
	} else if(where == 'ujdonsag') {
		limit= $('#ujdonsag_selector option:selected').val();
		magassag =( $('#ujdonsag_termekek').offset().top - 44);
		$('html, body').animate({scrollTop: magassag }, 500);
	} else {
		limit= $('#hamar_selector option:selected').val();
		magassag =( $('#hamarosan_termekek').offset().top - 44);
		$('html, body').animate({scrollTop: magassag }, 500);
	}

	if (getUrlVars()["anem"] != undefined) {
		if (getUrlVars()["anem"].substring(0,1) =="n" || getUrlVars()["anem"].substring(0,1) =="f") { var nem = "&anem=" + getUrlVars()["anem"].substring(0,1); }
	}
	else if (getUrlVars()["unem"] != undefined) {
		if (getUrlVars()["unem"].substring(0,1) =="n" || getUrlVars()["unem"].substring(0,1) =="f") { var nem = "&unem=" + getUrlVars()["unem"].substring(0,1); }
	}
	else {
		var nem = '';
	}
	
	$.ajax({
		type: "GET",
		url: "includes/ajaxshow.php?whatshow=" + where + "&page=" + what + "&limit=" + limit + nem,
		datatype: "html",
		success: function(data){
			$('#'+where+'_termekek').html(data);
			show_box();
		}
	});
	return false;
}


function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

//----------- SHOWBOX ------------

function show_box() {
	$(".showbox").mouseenter(function () {
		$('.commbox:visible').hide();
		$(this).prev(".commbox:hidden").fadeIn(100);
		$(this).prev(".commbox").css("z-index","1000");
		$(this).prev(".commbox").css("top",$(this).offset().top-120);
		$(this).prev(".commbox").css("left",$(this).offset().left);
	}).mouseleave(function () {
		$('.commbox').hide();
	});
	$('.commbox').mouseenter(function () {
		$('.commbox').hide();
	});
}


