//--- Variables
var test = "Test";
var popupActive = 0;
var infoAnimation = 0;
var myWidth;
var myHeight;
var productPos = 0;
var productImagePos = 0;
var productAnimation = 0;
var pageLink = window.location.href;
var browser = navigator.appVersion;
var messageSent = 0;
//--- Ready functions
$(document).ready(function(){	
	//setMenuBg();	
	menuHover();
	//Footer
	if($("#container").height() < 850){
		$("#footer p").css("color", "#FFF");
	}
	$("#logo.subLogo").click(function(){
		window.location = "../index.php";	
	});
	//Logo
	$("#subHeader #logo").click(function(){
		window.location = "../index.php";	
	});
	//- Slide
	if($("#slide").length > 0){
		$("#slide").orbit({
			directionalNav:true,
			bullets:true, 
			animationSpeed:1000,
			advanceSpeed:7000,
			timer:true	
		});
	}
	//Info panel navigation
	if($(".infoPanelNav").length > 0){
			infoPanelArrowHover();
			infoPanelNav();
		//Certification download
		certificationPopup();
		$("#actualities div, #freshReferences div").click(function(){
			var url = $(this).children("a").attr("href");
			window.location = url;	
		});
	}	
	//Menu
	$("#menu li").click(function(){
		var url = $(this).children("a").attr("href");
		window.location = url;	
	});
	//Submenu
	$("#subMenu>ul>li").hover(function(){
		if(!$(this).hasClass("active")){
			$(this).css({
				backgroundPosition:"0 -30px",
				cursor:"pointer"
			});
		}
	},function(){
		if(!$(this).hasClass("active")){
			$(this).css({
					backgroundPosition:"0 0",
					cursor:"default"
			});
		}
	});
	$("#subMenu>ul>li").click(function(){
		if($(this).children("a").attr("href") != undefined){
			var url = $(this).children("a").attr("href");
			window.location = url;	
		}
	});
	//Partners
	if($("#partners").length > 0){
		setPartnerBoxes();
		partnersHover();
	}
	//Products
	if($("#products").length > 0){
		product();
	}
	//Product
	if($("#product").length > 0){
		$(".productNav li").click(function(){
			var url = $(this).children("a").attr("href");
			window.location = url;	
		});
		$("#productDownload a").hover(function(){
			$(this).css({backgroundPosition:"0 -40px", color:"#FFF"});	
		},function(){
			$(this).css({backgroundPosition:"0 0px", color:"#081b34"});	
		});
		productChange();
	}
	//Product Thumbs
	if($("#productDownload #thumbs").length > 0){
		thumbs();
	}
	//Caterories
	if($(".categories").length > 0){
		categories();
		if(pageLink.indexOf("id") > -1){
			var id = pageLink.split("?").pop().split("=").pop();
			$("li.catLink").click();
			$("#"+id).click();
		} else {
			getReferences(0);
		}
	}
	//News
	if($("#news").length > 0){
		news();
	}
	//Associates
	if($("#associates").length > 0){
		associates();
	}
	//Send message
	if($("#sendMessage").length > 0){
		sendMessage();
	}
	//Downloads
	if($("#downloads").length > 0){
		downloads();
	}
	//Documents
	if($("#documents").length > 0){
		$("#documents div").click(function(){
			var url = $(this).children("a").attr("href");
			window.location = url;		
		});
	}
	//Video
	if($("#video").length > 0){
		video();
	}
	//Gallery
	if($("#gallery").length > 0){
		gallery();
	}
});
//--- Functions
function setMenuBg(){
	if($("#menu li.index").hasClass("active")){
		$("#headerBg").addClass("index");
	}
	if($("#menu li.aboutUs").hasClass("active")){
		$("#headerBg").addClass("aboutUs");
	}
	if($("#menu li.products").hasClass("active")){
		$("#headerBg").addClass("products");
	}
	if($("#menu li.references").hasClass("active")){
		$("#headerBg").addClass("references");
	}
	if($("#menu li.media").hasClass("active")){
		$("#headerBg").addClass("media");
	}
	if($("#menu li.contact").hasClass("active")){
		$("#headerBg").addClass("contact");
	}
}
function menuHover(){
	var index;
	var y;
	var left;
	var zIndex;
	$("#menu li").hover(function(){
		if(!$(this).hasClass("active")){
			index = $(this).index();
			y = index * (-38);
			left = ($(this).position().left) -10;
			zIndex = $(this).css("zIndex");
			$(this).css({
					width:"199px",
					height:"36px",
					left:left,
					top:"2px",
					backgroundPosition:"-169px " + y + "px",
					zIndex:10
			});	
		}
		},function(){
			if(!$(this).hasClass("active")){
			$(this).css({
					width:"169px",
					height:"30px",
					left:left+10,
					top:"8px",
					backgroundPosition:"0px " + y + "px",
					zIndex:zIndex
			});
		}
	});
}

function infoPanelArrowHover(){
	$(".infoPanelNav li").hover(function(){
			if($(this).hasClass("navUp")){
				$(this).children("a").css({backgroundPosition:"0 6px"});
			} else {
				$(this).children("a").css({backgroundPosition:"10px 6px"});
			}
		},function(){
			if($(this).hasClass("navUp")){
				$(this).children("a").css({backgroundPosition:"0 0px"});
			} else {
				$(this).children("a").css({backgroundPosition:"10px 0"});
		 }
	});
}

function infoPanelNav(){
	$(".infoPanelNav li").click(function(){
		//Variables
		var group = $(this).parent().attr("id");
		group = group.replace("Nav", "");
		var direction = $(this).attr("class");
		direction = direction.replace("nav", "").toLowerCase();
		//Actualities
		if(group == "actualities"){
			if(infoAnimation == 0){
				infoAnimation = 1;	
				$("#actualities div").eq(2).css({opacity:1});
				var div = "#actualities div";
				var first = $(div + ":first").clone();
				var last = $(div + ":last").clone();
				//Nav up
				if(direction == "up"){
					$(div).eq(1).fadeTo(500,0);
					$(div).eq(1).children("span").fadeTo(500,0);
					setTimeout(function(){
						$("#actualities").prepend(last);
						$("#actualities div:first").css({marginTop:"-60px"});
						$("#actualities div:first").animate({marginTop:"0px"},500);
					},500);
					setTimeout(function(){
						$("#actualities div").eq(2).css({opacity:1});	
						$("#actualities div").eq(2).children("span").css({opacity:1});	
					},1000);
					$(div + ":last").remove();	
				//Nav down
				} else {
					$(div  + ":first span").fadeTo(500,0);
					$(div + ":first").fadeTo(500,0).slideUp(500);
					setTimeout(function(){
						$("#actualities").append(first);
						$(div + ":first").remove();	
					},1000);
				}
				setTimeout(function(){
					infoAnimation = 0;	
				},1000);
			}
		} else {
			infoAnimation = 1;	
			$("#actualities div").eq(2).css({opacity:1});
			var div = "#freshReferences div";
			var first = $(div + ":first").clone();
			var last = $(div + ":last").clone();
			if(direction == "up"){
					$(div).eq(0).fadeTo(500,0);
					setTimeout(function(){
						$("#freshReferences").prepend(last);
						$("#freshReferences div:first").css({marginTop:"-125px"});
						$("#freshReferences div:first").animate({marginTop:"0px"},500);
					},500);
					setTimeout(function(){
						$("#freshReferences div").eq(1).css({opacity:1});	
					},1000);
					$(div + ":last").remove();	
				//Nav down
				} else {
					$(div + ":first").fadeTo(500,0).slideUp(500);
					setTimeout(function(){
						$("#freshReferences").append(first);
						$(div + ":first").remove();	
					},1000);
				}
				setTimeout(function(){
					infoAnimation = 0;	
				},1000);
		}
		return false;
	});
}

function certificationPopup(){
	$("#certifications span").hover(function(){
		var certifications = new Array(["images/certifications/certop.jpg", "docs/certifications/bartex_certop_tanusitvany_hu_eng.pdf"],
																	 ["images/certifications/apeh.jpg", "docs/certifications/bartex_apeh_ap_707_tanusitvany.pdf"],
																	 ["",""],
																	 ["images/certifications/hungaropark.jpg", "docs/certifications/bartex_hungaropark_tanusitvany.pdf"]);
		var index = $(this).parent().index();
		if(index != 2){
			var positions = new Array(10,245,485,720);
			var left = positions[index];
			var downloadPanel = "<div id='popupHolder'>" +
													"<div id='downloadPopup'>" +
													"<img src='" + certifications[index][0] + "' width='154' height='212' />" +
													"<a href='" + certifications[index][1] + "'>Tanusítvány letöltése</a>" +
													"</div></div>"
			if(popupActive == 0){
				$(this).addClass("active");
				$("#content").append(downloadPanel);	
				$("#popupHolder").css("left", left);
				popupActive = 1;
			}	
		}
	},function(){
		closePopup();
	});
	
}

function closePopup(){
	$("#popupHolder").mouseleave(function(){
		$("#popupHolder").remove();
		popupActive = 0;	
	});
}

function getResolution() {
	if (typeof(window.innerWidth) == 'number') {
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
}

function setPartnerBoxes(){
	var x = 0;
	var y = 0;
	for(var i = 0; i < 12; i++){
		var moduloX = i % 3;
		if(moduloX == 0){
			x = 0;
		} else if(moduloX == 1){
			x = 240;
		} else {
			x = 480;
		}
		var y = (Math.floor(i / 3)) * 140;
		$("#partners div").eq(i).css({
			left:x + "px",
			top:y + "px"
		});
	}
}

function partnersHover(){
	$("#partners div").hover(function(){
		$(this).css({
			backgroundPosition:"0 -140px",
			zIndex:100
		});
		$(this).animate({height:"280px"},300);
		$(this).children("p").css("display", "block");	
	},function(){
		$(this).stop().css({
							height:"116px",
							backgroundPosition:"0 0",
							zIndex:0
		});		
		$(this).children("p").css("display", "none");
	});
}

var products = new Array();
function product(){
	var id = window.location.href;
	id = id.split("?").pop().split("=").pop();
	var url = "../includes/getproducts.php";
	var dataString = "id=" + id;
	$.ajax({
		url:url,
		type:"post",
		data:dataString,
		success:function(data){
			products = data.split("#");
			for(var i = 0; i < products.length; i++){
				products[i] = products[i].split(",");
			}
			var last = (products.length)-1;
			if(products.length == 1){
				$("#productsImageHolder").append("<div><img width='218px' height='250' src='" + products[0][2] + "' /></div>" + 
																					"<div><img width='218px' height='250' src='" + products[0][2] + "' /></div>" + 
																					"<div><img width='218px' height='250' src='" + products[0][2] + "' /></div>");
				$("#productsFooter a").html(products[0][1]).attr("href","termek.php?id=" + products[0][0])

			} else {
				$("#productsImageHolder").append("<div><img width='218px' height='250' src='" + products[last][2] + "' /></div>" + 
																					"<div><img width='218px' height='250' src='" + products[0][2] + "' /></div>" + 
																					"<div><img width='218px' height='250' src='" + products[1][2] + "' /></div>");
				$("#productsFooter a").html(products[0][1]).attr("href","termek.php?id=" + products[0][0])
			}
		}
	});
	//Header
	$("#productsHeader li").hover(function(){
		if($(this).hasClass("navLeft")){
			$(this).children("a").css("backgroundPosition", "0 -10px");
		} else {
			$(this).children("a").css("backgroundPosition", "-6px -10px");
		}
	},function(){
		if($(this).hasClass("navLeft")){
			$(this).children("a").css("backgroundPosition", "0 0");
		} else {
			$(this).children("a").css("backgroundPosition", "-6px 0");
		}
	});
	$("#productsHeader li").click(function(){
		if(productAnimation == 0){
			var direction = $(this).attr("class");
			direction = direction.toLowerCase().replace("nav","");
			if(direction == "left"){
				if(productPos == 0){
					productPos = (products.length)-1;
				} else {
					productPos--;
				}	
				if(productPos == 0){
					productImagePos = products.length-1;
				} else {
					productImagePos = productPos-1;
				}
			} else {
				if(productPos == (products.length)-1){
					productPos = 0;
				} else {
					productPos++;
				}
				if(productPos == products.length-1){
					productImagePos = 0;
				} else {
					productImagePos = productPos+1;
				}
			}
			var href = products[productPos][0];
			var text = products[productPos][1];
			var image = products[productImagePos][2];	
			productAnimation = 1;	
			if(direction == "left"){
				$("#productsImageHolder").css({width:"872px",marginLeft:"-436px"}).prepend("<div><img width='218' height='250' src='" + image + "'/></div>");
				$("#productsImageHolder").animate({marginLeft:"-218px"},300);
			} else {
				$("#productsImageHolder").css({width:"872px"}).append("<div><img width='218' height='250' src='" + image + "' /></div>");
				$("#productsImageHolder").animate({marginLeft:"-436px"},300);
			}
			$("#productsFooter a").attr("href", "termek.php?id=" + href).html(text);		
			if(direction == "left"){
				setTimeout(function(){
					$("#productsImageHolder").css({width:"654px"});
					$("#productsImageHolder div").eq(3).remove();
					productAnimation = 0;
				},500);
			} else {
				setTimeout(function(){
					$("#productsImageHolder").css({width:"654px",marginLeft:"-218px"});
					$("#productsImageHolder div").eq(0).remove();
					productAnimation = 0;
				},500);
			}
		}
	});
	//Footer
	$("#productsFooter, #productsImageHolder").hover(function(){
			$("#productsFooter").css("backgroundPosition","0 -30px");
			$("#productsFooter a").css("color", "white");
		}, function(){
			$("#productsFooter").css("backgroundPosition","0 0");
			$("#productsFooter a").css("color", "#081B34");
	});
	$("#productsFooter, #productsImageHolder").click(function(){
		var href = $("#productsFooter a").attr("href");
		window.location = href;
	});
	//Sub box
	if($("#subBoxHeader li").length > 0){
		var widths = 0;
		for(var i = 0; i < $("#subBoxHeader li").length; i++){
			widths += Math.ceil($("#subBoxHeader li").eq(i).width())+1;
		}
		var width = ((459-widths)/4);
		for(var i = 0; i < $("#subBoxHeader li").length; i++){
			var currWidth = Math.ceil($("#subBoxHeader li").eq(i).width());
			$("#subBoxHeader li").eq(i).css("width",currWidth + width);
		}
	}
	
	$("#subBoxHeader li").click(function(){
		if(!$(this).hasClass("active")){
			var index = $(this).index();
			$("#subBoxHeader li.active").removeClass("active");
			$(this).addClass("active");
			$("#subBoxContent div.active").removeClass("active");
			$("#subBoxContent div").eq(index).addClass("active");
		}
	});
}

//Categories
function categories(){
	$(".categories li div").hide();
	$(".categories li.catLink").click(function(){
		$(".categories>li:first-child").css("backgroundPosition", "0 0px");	
		$(this).children("div").slideDown(300);
		$("#subMenu>ul>li.active").removeClass("active");
		$(this).addClass("active");
		$(this).css("backgroundPosition", "0 -210px");	
	});
	$(".categories div li").click(function(){
		var type = $(this).attr("id");
		getReferences(type);
		$(".categories>li>div>ul>li.active").removeClass("active");
		$(this).addClass("active");
	});
	$("#0").click(function(){
			$(".categories li.active").removeClass("active");
			$(".catLink").css("backgroundPosition", "0 0px");	
			$(this).addClass("active").css("backgroundPosition", "0 -60px");
			getReferences(0);
			$(".categories .catLink div").slideUp(300);
	});
}

function getReferences(type){
	if(browser.indexOf("MSIE 7.0") == -1 && browser.indexOf("MSIE 8.0") == -1){
		$(".refHolder").animate({opacity:0},500);
	}
	var url = "../includes/getreferences.php";
	var dataString = "type=" + type;
	$.ajax({
		url:url,
		type:"post",
		data:dataString,
		success:function(data){
			$("#references").html(data);		
			$("#references div div.description, #references div div.bottom").css("display", "none");
			var boxes = $("#references div.refHolder").length;
			if(boxes < 7){
				$("#subContentMiddle.references").animate({height:"420px"},300);
			} else {
				var height = ((Math.ceil(boxes / 3)) * 200)+20;
				$("#subContentMiddle").animate({height:height},300);
			}
			setTimeout(function(){
				if(browser.indexOf("MSIE 7.0") == -1 && browser.indexOf("MSIE 8.0") == -1){
					$(".refHolder").animate({opacity:1},500);
				}
			},300);
			setRefBoxes();
			refHover();
		}
	});
}

function refHover(){
	$("#references div.refHolder").hover(function(){
		$(this).children(".bottom").show();		
		$(this).children(".top").css("backgroundPosition", "0 -204px");
		$(this).children(".top").children(".description").slideDown(300);	
		$(this).css("zIndex",100).addClass("active");
	},function(){
		$(this).children(".bottom").hide();	
		$(this).children(".top").css("backgroundPosition", "0 0");
		$(this).children(".top").children(".description").hide();	
		$(this).css("zIndex",0).removeClass("active");
	});
}

function setRefBoxes(){
	var x = 0;
	var y = 0;
	var boxes = $("#references div.refHolder").length;
	for(var i = 0; i < boxes; i++){
		var moduloX = i % 3;
		if(moduloX == 0){
			x = 0;
		} else if(moduloX == 1){
			x = 240;
		} else {
			x = 480;
		}
		var y = (Math.floor(i / 3)) * 204;
		$("#references div.refHolder").eq(i).css({
			left:x + "px",
			top:y + "px"
		});
	}
}
function productChange(){
	resizeProductBox();
	$("#leftLink").click(function(){
			if(!$("#left").hasClass("active")){
				$("#right,#rightLink").removeClass("active");
				$("#left,#leftLink").addClass("active");
			}
			resizeProductBox();
	});
	$("#rightLink").click(function(){
			if(!$("#right").hasClass("active")){
				$("#left, #leftLink").removeClass("active");
				$("#right, #rightLink").addClass("active");
			}
			resizeProductBox();
	});
}

function resizeProductBox(){
	if($("#parametersContent div.active ul").length > 0){
		var height = $("#parametersContent div.active ul").height();
	} else {
		var height = $("#parametersContent div.active p").height();
	}
	$("#parametersContent").css("height", height);
}
//News
function news(){
	newsHover();
	$("#news div span.more").click(function(){
		if(!$(this).hasClass("active")){
			$(this).addClass("active");
			$(this).html("vissza").css({backgroundPosition:"0 -30px"});
			var height = $(this).parent().parent().children(".newsContent").children("p").height();
			$(this).parent().parent().animate({height:height+60},300);	
			$(this).parent().parent().children(".newsContent").animate({height:height},300);	
			newsHover();
		} else {
			$(this).removeClass("active");
			$(this).html("tovább").css({backgroundPosition:"0 0px"});
			$(this).parent().parent().animate({height:"106px"},300);	
			$(this).parent().parent().children(".newsContent").animate({height:"45px"},300);	
			newsHover();
		}
	});
}

function newsHover(){
	$("#news div span.more").not(".active").hover(function(){
		$(this).css({backgroundPosition:"0 -15px"});	
	},function(){
		$(this).css({backgroundPosition:"0 0"});
	});
	$("#news div span.more.active").hover(function(){
		$(this).css({backgroundPosition:"0 -45px"});	
	},function(){
		$(this).css({backgroundPosition:"0 -30px"});
	});
}

//Associates
function associates(){
	$(".telLink").hover(function(){
		$(this).parent().parent().children(".tel").css({visibility:"visible"});	
	},function(){
		if(!$(this).hasClass("active")){
			$(this).parent().parent().children(".tel").css({visibility:"hidden"});	
		}
	});
	$(".telLink").click(function(){
		$(this).addClass("active");
		$(this).parent().parent().children(".tel").css({visibility:"visible"});	
	});
	$(".emailLink").click(function(){
		var index = $(this).parent().parent().index()+1;
		window.location = "../kapcsolat/uzenetkuldes.php?id=" +index;	
	});
}

function sendMessage(){
	if(messageSent == 0){
		setInitValues();
		$("#sendMessage input[type=text], #sendMessage textarea").focus(function(){
				var id = $(this).attr("id");
				if($(this).val() == initValues[id]){
					$(this).val("");
				}
		});
		$("#sendMessage input[type=text], #sendMessage textarea").blur(function(){
			var id = $(this).attr("id");
			if($(this).val() == ""){
				$(this).val(initValues[id]);
			}
		});
		$("#submit").click(function(){
			if(pageLink.indexOf("id") != -1){
				var id = pageLink.split("?").pop().split("=").pop();
			} else {
				var id = 0;
			}
			var name = $("#name").val();
			var company = $("#company").val();
			var tel = $("#tel").val();
			var email = $("#email").val();
			var subject = $("#subject").val();
			var message = $("#message").val();
			var url = "mail.php";
			var dataString = "id=" + id + "&name=" + name + "&company=" + company + "&tel=" + tel + "&email=" + email + "&subject=" + subject + "&message=" + message;
			$.ajax({
				url:url,
				type:"post",
				data:dataString,
				success:function(data){
					if(data == "Az üzenetet sikeresen elküldte!"){
						messageSent = 1;
					}
					$("p.feedback").html(data);
				}
			});
			return false;
		});
	}
}

var initValues = new Array();
function setInitValues(){
	var inputLength = $("#sendMessage input").length;
	for(var i = 0; i < inputLength-1; i++){
		var id = $("#sendMessage input[type=text]").eq(i).attr("id");
		initValues[id] = $("#sendMessage input[type=text]").eq(i).val();
	}
	initValues["message"] = $("#sendMessage textarea").val();
}

function downloads(){
	$("#downloads div").click(function(){
			var url = $(this).children("a").attr("href");
			window.location = url;	
	});
}

var pos = 0;
function setActiveContent(){
	pos = $(".orbit-bullets li.active").index();
	if(browser.indexOf("MSIE 7.0") == -1 && browser.indexOf("MSIE 8.0") == -1){
		$("#slideBox>div.active").animate({opacity:0},500);
		setTimeout(function(){
			$("#slideBox>div").eq(pos).css("display", "block").animate({opacity:1},500);		
		},500);
		setTimeout(function(){
			$("#slideBox>div.active").removeClass("active").css("display", "none");		
			$("#slideBox>div").eq(pos).addClass("active");		
		},1000);
	} else {
		setTimeout(function(){
			$("#slideBox>div").eq(pos).css("display", "block");		
			$("#slideBox>div.active").removeClass("active").css("display", "none");		
			$("#slideBox>div").eq(pos).addClass("active");	
		},1000);
	}
}

function video(){
	$("#video a").hover(function(){
		$(this).siblings("span").css("backgroundPosition", "0 -48px");
	}, function(){
		$(this).siblings("span").css("backgroundPosition", "0 0");
	});
	$("#video span.play").hover(function(){
		$(this).css("backgroundPosition", "0 -48px");
	}, function(){
		$(this).css("backgroundPosition", "0 0");
	});
	$("#video span.play").click(function(){
		$(this).siblings("a").click();
	});
}

function gallery(){
	$("#gallery div").click(function(){
		var url = $(this).children("a").attr("href");
		window.location = url;	
	});
}

function thumbs(){
	$("#productDownload #thumbs div img").click(function(){
		$("#productDownload #thumbs div.active").removeClass("active");
		$(this).parent().addClass("active");	
		var src = $(this).attr("src");
		src = src.replace("_thumb","");
		$("#productDownload img.productImage").attr("src",src);
	});
}
