
$(function(){

	$(document).ready(function(){
		$("ul#importantlinks li#contact_intro a.more2, ul#importantlinks li#contact_intro input.contact_form_intro").click(function(){
			$("ul#importantlinks li#contact_intro").hide().parent().find("li#contact").show().find("input[name=name]").focus();
		});
		$("ul#importantlinks li#contact a.back").click(function(){
			$("ul#importantlinks li#contact_intro").show().parent().find("li#contact").hide();
		});
		
		$("ul#importantlinks li#contact_intro a.more2").hover(function(){
			$(this).css({color: '#fff'}).parent().find("span.more2").css({opacity: 0.4});
		},function(){
			$(this).css({color: '#109672'}).parent().find("span.more2").css({opacity: 0.1});
		});
		
		$("div#contactconfirm a.btn1").hover(function(){
			$(this).css({color: '#fff'}).parent().find("span.btn1[rel="+$(this).attr("rel")+"]").css({opacity: 0.4});
		},function(){
			$(this).css({color: '#109672'}).parent().find("span.btn1[rel="+$(this).attr("rel")+"]").css({opacity: 0.1});
		});
		

		// --- stat ---
		$("ul#content ul#importantlinks li#contact_intro a.more2").click(function() {
			stat('count_reg_click')
		});

		$("ul#header li#doublemenu div#mainmenu a").click(function() {
			stat($(this).attr('href'));
		});
		
		
		if ($("ul#importantoffer").length!=0) {
			var contactconfirmLeft = $("ul#importantoffer").position().left + ($("ul#importantoffer").width()/2) - ($("div#contactconfirm").width()/2),
				contactconfirmTop = $("ul#importantoffer").position().top + 40;
			$("div#contactconfirm").css({ top: contactconfirmTop+'px', left: contactconfirmLeft+'px' });
			
			
			$("div#contactconfirm a.back").click(function(){ $("div#contactconfirm").hide(); });
			$("li#kapcsolatfelvetel a").click(function(){ $("div#contactconfirm").show(); $("div#contactconfirm #state1").show();  $("div#contactconfirm #state2").hide(); return false; });
			$("div#contactconfirm a.btn1_1").click(function(){ $("div#contactconfirm #state1").toggle();  $("div#contactconfirm #state2").toggle(); });
		}
		
		$("input#updateChart").click(function() {

			$.ajax({
				   url: "index/arfolyamajax?"+$("#arfolyamform").serialize(),
				   dataType: "json",
				   success: function(data){
						if (data.result) {
							swfobject.embedSWF("/i/open-flash-chart.swf", "my_chart", "542", "230", "9.0.0", "expressInstall.swf", {"data-file":"/index/arfolyamdata/"+replaceAll(replaceAll($("#arfolyamform").serialize(),"&","/"),"=","/")}, {bgcolor: "#e1dedc"});
						} else {
							alert("A kiválasztott kezdődátumhoz vagy végdátumhoz nem tartozik árfolyamadat. Kérjük válasszon másikat!");	
						}
					}
			});

		});
		
		$("form#arfolyamform fieldset.interval span.item").each(function(){
			 if ($(this).find("select").length!=0) {
				$(this).append('<span class="select" rel="'+$(this).find("select").attr("name")+'">'+$(this).find("select").val()+'</span>');
				$(this).find("select").change(function(){ $(this).parent().find("span.select").text($(this).val()); });
			}
		});
		
		$("div.currency_selector ul.currency_tabs li a").each(function(){
			$(this).click(function(){
				if (!$(this).hasClass("active")) {
					$(this).closest("ul").find("a").removeClass("active");
					$(this).addClass("active");
					$("div.currency_selector div.currency_table").removeClass("active");
					$("div.currency_selector div#currency_" + $(this).attr("id").split("_")[1] + "_table").addClass("active");
				}
			});
		});
		
		
	});
		  
});

function replaceAll(strText,strTarget,strSubString) {
  var intIndexOfMatch = strText.indexOf(strTarget);
  while (intIndexOfMatch != -1) {
   strText = strText.replace(strTarget, strSubString)
   intIndexOfMatch = strText.indexOf(strTarget);
  } 
  return( strText );
}


function BannerRotator(i,o) {
	if (!o) { o = {}; }
	var opts = $.extend({
		tab_pfx: "bnrtb_",
		banner_pfx: "bnrad_"
	},o);
	
	if (i!="auto" && BannerRotatorActive!=i) {
		$("div.banners div.banner").removeClass("active");
		$("div#"+opts.banner_pfx+i).addClass("active");
		$("ul.bannertabs a").removeClass("active");
		$("ul.bannertabs a#"+opts.tab_pfx+i).addClass("active");
		BannerRotatorActive = i;
		clearTimeout(BannerRotatorTo);
		BannerRotatorTo = setTimeout(function(){ BannerRotator("auto"); },BannerTiming[i-1]*1000);
	}
	
	if (i=="auto") {
		BannerRotator((parseInt(BannerRotatorActive)+1 > $("div.banners div.banner").length) ? 1 : parseInt(BannerRotatorActive)+1)
	}
}

var _rotator;
function ProductRotator(count, interval) {
	this.count = count-1;
	this.interval = interval * 1000;
	this.current = 0;
}

ProductRotator.prototype = {
	start: function() {
		_rotator = this; //because of IE doesn't support the third parameter of setInterval
		setInterval(this.rotate, this.interval);
	},
	
	rotate: function() {
		var curr = _rotator.current;
		//$('#importantcreditoffer .topproduct:eq(0)').fadeOut('slow');
		$('#importantcreditoffer .topproduct').get(curr).style.display = 'none';
		_rotator.current++;
		if (_rotator.current > _rotator.count) {
			_rotator.current = 0;
		}
		curr = _rotator.current;
		$('#importantcreditoffer .topproduct').get(curr).style.display = 'block';
		//$('#importantcreditoffer .topproduct:eq(0)').fadeIn('slow');
	}
}

function showProd(id, currency) {
	$('#paymodes a').removeClass();
	$('#paymode_'+currency+'_'+id).addClass('current');

	var curr = document.getElementById('prod_'+currency+'_'+visible);
	if (curr) {
		curr.style.display = 'none';
	}
	curr = document.getElementById('graph_'+visible);
	if (curr) {
		curr.style.display = 'none';
	}	
	var tab = document.getElementById('prod_'+currency+'_'+id);
	if (tab) {
		tab.style.display = document.all ? 'block' : 'table';
	}
	tab = document.getElementById('graph_'+id);
	if (tab) {
		tab.style.display = 'block';
	}
	visible = id;
	return false;
}

function setTooltipCookie(show) {
	$.cookie('showtooltip', show ? 1 : 0, {expires: 365, path: '/'});
}

function getTooltipCookie() {
	cookie = $.cookie('showtooltip');
	if (cookie == null) {
		setTooltipCookie(true);
	}
	return (cookie == null) ? 1 : (cookie == '1' ? true : false);
}

function showTooltip(show) {
	//this.blur();
	show_tooltip = show;
	setTooltipCookie(show);
	if (show) {
		$('#showtooltip').html('magyarázat ki');
		$('.tooltipswitcher').get(0).style.backgroundPosition = "right bottom";
	} else {
		$('#showtooltip').html('magyarázat be');
		$('.tooltipswitcher').get(0).style.backgroundPosition = "right top";
	}
}

function getCities(region) {
	$('#tk_cities').load('hiteltanacsadok', {region: region}, function() {
		$('#tk').show();
		//$('#region_name').html(region + 'i');
		$('#tk_names').html('');
	});
}

var current_city = '';
function getAgents(evt, city_id) {
	evt = evt || window.event;
	var src = evt.target || evt.srcElement;
	if (current_city != '') {
		current_city.className = '';
	}
	src.className = 'selected';
	$('#tk_names').load('hiteltanacsadok', {city: city_id});
	current_city = src;
	return false;
}

function winOpen(url,n,w,h) {
    var sw = screen.width;
    var sh = screen.height;

    if(w=="*") {
        w = screen.width-100;
        var left = sw ? (sw-w)/2 : 0;
    } else {
        var left = sw ? (sw-w)/2 : 0;
    }

    if(h=="*") {
        h = screen.height-100;
        var top = 0;
    }else {
        var top = sh ? (sh-h)/2 : 0;
    }

    window.open(url,n,"width=" + w + ",height=" + h + ",top=" + top + ",left=" + left + ",status=0,toolbar=0,scrollbars=yes");
}

function jumpNextField(event, length, obj, fieldname) {
	var event = event || window.event;
	var char = event.which || event.keyCode;

	if (char >= 32 && obj.value.length == length) {
		document.getElementById(fieldname).focus();
	} 
}

function textareaMaxlength(id, length) {

	var keyfunc = function() {
		if (this.value.length > length) {
			this.value = this.value.substr(0, length);
			return false;
		}
	};

	$('#' + id).keyup(keyfunc).keydown(keyfunc).blur(keyfunc);
}

function stat(field) {
	$.post('index/stat', {field: field});
}
