$(document).ready(function() {

/* 
 * ----------------------------------------------------------------------------- 
 * box hover effekt
 * -----------------------------------------------------------------------------
 */
	$('div.hoverbox').each(function(){
		var effectclass=$(this).attr('title');
		$(this).attr('title','');
		var aeffectclass=effectclass.split('_');
		$(this).addClass(aeffectclass[0]);
		$(this).hover(function(){ 
			$(this).removeClass();
			$(this).addClass(aeffectclass[1]);
		},function(){ 
			$(this).removeClass();
			$(this).addClass(aeffectclass[0]); 
		});
		
	});
	
/* 
 * ----------------------------------------------------------------------------- 
 * menu
 * -----------------------------------------------------------------------------
 */
	var hideMenu;
	$('ul.menu li').each(function(){
		$(this).hover(function(){
			clearTimeout(hideMenu);
			$('ul.menu li').removeClass('selected');
			$(this).addClass('selected');
			$('div#submenu_container ul.menu').hide(0);
			$('ul.submenu'+$("li").index(this)).show(0);
			if($.browser.msie) {
				$('ul.submenu'+$("li").index(this)).css('display','inline');
			}
/*			$('ul.submenu'+$("li").index(this)).animate({ 
				height: 'toggle', opacity: 'toggle' 
			}, "slow");*/
		},function(){ 
			hideMenu=setTimeout(function(){
				$('ul.menu li').removeClass('selected');
				$('div#submenu_container ul.menu').hide(0);
			},500);
		});
	});

/* 
 * ----------------------------------------------------------------------------- 
 * submenu
 * -----------------------------------------------------------------------------
 */
 
 	
	$('div#submenu_container ul.menu li').hover(
		function(){
			
			$(this).parents('ul').show(0);
			if($.browser.msie) {
				$(this).parents('ul').css('display','inline');
			}
			$('ul.menu').eq(0).find('li:eq('+$(this).parents('ul').attr('class').substr($(this).parents('ul').attr('class').length-1)+')').addClass('selected');
		},
		function(){
			/*hideMenu=setTimeout(function(){
				$('ul.menu li').removeClass('selected');
				$(this).parents('ul').hide(0);
			},500);*/
		}
	);

/* 
 * ----------------------------------------------------------------------------- 
 * select listak 
 * -----------------------------------------------------------------------------
 */
 	$('select option').each(function(i){
 		if(i%2==0) {
 			$(this).css('background-color','#EAEAEA');
 		} else {
 			$(this).css('background-color','#F3F3F3');
 		}
 	});

/* 
 * ----------------------------------------------------------------------------- 
 * hot topics scroll
 * -----------------------------------------------------------------------------
 */
 	if($('#hottopics p').length>2) {
 		var linksheight=0;
 		$('#hottopics p').each(function(){
 			linksheight+=parseInt($(this).height())+5;
 		});
	 	$('#hottopics div.scroll div').css('height',linksheight);
	 	$('#hottopics div.scroll div').clone().appendTo('#hottopics div.scroll');
		
		$('#hottopics div.scroll').mouseout(function(e){
			linksScrollTimer('up');
		});
		
		$('#hottopics div.scroll').mouseover(function(e){
			if(typeof timer!='undefined') {
				clearInterval(timer);
			}
		});
		
		$('#hottopics div.scroll a').mousemove(function(e){
			if(typeof timer!='undefined') {
				clearInterval(timer);
			}
			
		});
	
		function linksScrollTimer(dir){
			if(dir=='up') {
				var sEq='0';
				var sChange=1;
			} else {
				var sEq='1';
				var sChange=-1;
			}
			timer=setInterval(function(){
				var curTop=$('#hottopics div.scroll div:eq(0)').css('margin-top');
				$('#hottopics div.scroll div:eq(0)')[0].style.marginTop=parseInt(curTop)-sChange+'px';
				if(Math.abs(parseInt(curTop))>linksheight && dir=='up') {
					$('#hottopics div.scroll div:eq(0)').css('margin-top',0);
				}
				if(parseInt(curTop)>-1 && dir=='down') {
					$('#hottopics div.scroll div:eq(0)').css('margin-top',linksheight*-1);
				}
		 	},200);
				
		}
		linksScrollTimer('up');
 	}
	


/* 
 * ----------------------------------------------------------------------------- 
 * banners
 * -----------------------------------------------------------------------------
 */
 
 	var charts={charts: [
		{
			id: 'chart_smallchart',
			flash: '/public/charts/small_chart_POST.swf',
			width:275,
			height:182,
			scale: 'exactfit'
		},
		{
			id: 'chart_pricetable',
			flash: '/public/charts/pricetable.swf',
			width:190,
			height:182,
			scale: 'noscale'
		},
		{
			id: 'clock1',
			objid: 'NewYork',
			flash: '/public/flash/clock.swf',
			width:47,
			height:47,
			scale: 'noscale'
		},
		{
			id: 'clock2',
			objid: 'London',
			flash: '/public/flash/clock.swf',
			width:47,
			height:47,
			scale: 'noscale'
		},
		{
			id: 'clock3',
			objid: 'Tokyo',
			flash: '/public/flash/clock.swf',
			width:47,
			height:47,
			scale: 'noscale'
		},
		{
			id: 'date',
			objid: 'date',
			flash: '/public/flash/date.swf',
			width:70,
			height:47,
			scale: 'noscale'
		},
		{
			id: 'popup_smallchart',
			flash: '/public/charts/small_chart_POST.swf',
			width:258,
			height:182,
			scale: 'noscale'
		},
		{
			id: 'popup_bigchart',
			flash: '/public/charts/BigChart-noclose.swf',
			width:700,
			height:505,
			scale: 'noscale'
		}

	]};
	
	for(var i in charts.charts) {
		var flashhtml=$('<object id="'+charts.charts[i].objid+'" name="'+charts.charts[i].objid+'" type="application/x-shockwave-flash" data="'+charts.charts[i].flash+'" width="'+charts.charts[i].width+'" height="'+charts.charts[i].height+'"><param name="movie" value="'+charts.charts[i].flash+'"></param><param name="allowScriptAccess" value="sameDomain"></param><param name="allowFullScreen" value="true"></param><param name="quality" value="high"></param><param name="scale" value="'+charts.charts[i].scale+'"></param></object>');
		$('#'+charts.charts[i].id).parent().append(flashhtml[0]);

		if(i==charts.charts.length-1){
			$('div.chart').remove();
		}
	}

/* 
 * ----------------------------------------------------------------------------- 
 * icon buttons
 * -----------------------------------------------------------------------------
 */

	var buttons={buttons: [
		{
			id:'opensmallchart',
			type:'popup',
			url:'/templates/html/popups/smallchart.html',
			width:258,
			height:250
		},
		{
			id:'openbigchart',
			type:'popup',
			url:'/templates/html/popups/bigchart.html',
			width:700,
			height:570
		},
		{
			id:'rssreader',
			type:'popup',
			url:'/public/flash/rssreader.swf',
			width:468,
			height:180
		},
		{
			id:'rssreader2',
			type:'popup',
			url:'/public/flash/rssreader.swf',
			width:468,
			height:180
		},
		{
			id:'pricebanner',
			type:'popup',
			url:'/templates/html/popups/pricetable_v.html?uid=1234',
			width:163,
			height:408
		},
		{
			id:'pricebanner2',
			type:'popup',
			url:'/templates/html/popups/pricetable_v.html?uid=1234',
			width:163,
			height:408
		},
		{
			id:'pricebanner3',
			type:'popup',
			url:'/templates/html/popups/pricetable_v.html?uid=1234',
			width:163,
			height:408
		},
		{
			id:'advcconv',
			type:'popup',
			url:'/tools/advancedconverter/',
			width:465,
			height:223
		}
	]};
	
	for(b in buttons.buttons) {
		$('#'+buttons.buttons[b].id).bind("click", {button: buttons.buttons[b]}, buttonClickHandler);
	}
	
	function buttonClickHandler(event) {
		switch (event.data.button.type) {
			case 'newwindow':
				window.open(event.data.button.url);
				break;
			case 'selfwindow':
				window.location.href=event.data.button.url;
				break;
			case 'popup':
				window.open(event.data.button.url,'popup_'+event.data.button.name,'width='+event.data.button.width+',height='+event.data.button.height);
				break;
		}
	}



/* titles megoldas, elavulva	
 * $('img.icon').each(function(){
 		eval('var btnbehav='+$(this).attr("title"));
		if($(this).attr("title")!=undefined) {
			$(this).click(function(){
				
				switch (btnbehav.type) {
					case 'newwindow':
						window.open(btnbehav.url);
						break;
					case 'selfwindow':
						window.location.href=btnbehav.url;
						break;
					case 'popup':
						window.open(btnbehav.url,'popup_'+btnbehav.name,'width='+btnbehav.width+',height='+btnbehav.height);
						break;
				}
			 
			});
		}
		$(this).attr("title", ""); 
	
 	});*/
 	
	
/* 
 * ----------------------------------------------------------------------------- 
 * rotating banners
 * -----------------------------------------------------------------------------
 */
	$.getJSON("/bannerlist/", function(json){ 

		var rotateBanner = function(thisbanner){
			$('#'+json.flashbanners[thisbanner].divid).unbind();
			$('#'+json.flashbanners[thisbanner].divid).find('div,object').remove();
			json.flashbanners[thisbanner].selected=json.flashbanners[thisbanner].selected || 0;
			if(json.flashbanners[thisbanner].selected >= json.flashbanners[thisbanner].flash.length-1) {
				json.flashbanners[thisbanner].selected=0;
			} else {
				json.flashbanners[thisbanner].selected++;
			}
			var currentbanner = json.flashbanners[thisbanner].selected;
			var banner=$('#'+json.flashbanners[thisbanner].divid);
			if(banner.length>0) {
				if(json.flashbanners[thisbanner].flash[currentbanner].swf!='none') {
					var arr_ext=new Array;
					arr_ext=json.flashbanners[thisbanner].flash[currentbanner].swf.split('.');
					var file_ext=arr_ext[arr_ext.length-1];
					
					if(file_ext=="swf") {
						/* swf */
						var tmphtml='<div class="transparent"><div class="abs" style="height:'+json.flashbanners[thisbanner].flash[currentbanner].height+'px;line-height:'+json.flashbanners[thisbanner].flash[currentbanner].height+'px;width:'+json.flashbanners[thisbanner].flash[currentbanner].width+'px;"></div><object type="application/x-shockwave-flash" data="'+json.flashbanners[thisbanner].flash[currentbanner].swf+'" width="'+json.flashbanners[thisbanner].flash[currentbanner].width+'" height="'+json.flashbanners[thisbanner].flash[currentbanner].height+'"><param name="movie" value="'+json.flashbanners[thisbanner].flash[currentbanner].swf+'" /><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="true" /><param name="quality" value="high" /><param name="scale" value="noscale" /><param name="wmode" value="opaque" /></object></div>';
						banner[0].innerHTML=tmphtml;
					} else {
						/* image */
						var tmphtml='<div class="transparent"><div class="abs" style="height:'+json.flashbanners[thisbanner].flash[currentbanner].height+'px;line-height:'+json.flashbanners[thisbanner].flash[currentbanner].height+'px;width:'+json.flashbanners[thisbanner].flash[currentbanner].width+'px;"></div><img src="'+json.flashbanners[thisbanner].flash[currentbanner].swf+'" alt="" /></div>';
						banner[0].innerHTML=tmphtml;
					}
					
					
				} else {
					/* html */
					banner[0].innerHTML=json.flashbanners[thisbanner].flash[currentbanner].html;
				}
				
				
				banner.click(function(){
					switch (json.flashbanners[thisbanner].flash[currentbanner].popup.type) {
						case 'newwindow':
							window.open(json.flashbanners[thisbanner].flash[currentbanner].url);
							break;
						case 'selfwindow':
							window.location.href=json.flashbanners[thisbanner].flash[currentbanner].url;
							break;
						case 'popup':
							window.open(json.flashbanners[thisbanner].flash[currentbanner].url,'popup_'+json.flashbanners[thisbanner].divid,'width='+json.flashbanners[thisbanner].flash[currentbanner].popup.width+',height='+json.flashbanners[thisbanner].flash[currentbanner].popup.height);
							break;
					}
				});
				setTimeout(function(){
					rotateBanner(thisbanner);
					$.get("/bannermegjelenesURL", { id: json.flashbanners[thisbanner].flash[currentbanner].id } );
				},json.flashbanners[thisbanner].flash[currentbanner].visible*1000);
			}
			
		}
		
		for(j=0;j<json.flashbanners.length;j++) {
			rotateBanner(j);
		}
		
	});

/* 
 * ----------------------------------------------------------------------------- 
 * tabos tablazatok
 * -----------------------------------------------------------------------------
 */
	$('#tabcontainer').click(function(e){
		if($(e.target).is("div.tab")) {
			$(e.target).siblings().removeClass('selected');
			$(e.target).addClass('selected');
			var tbodyindex=$("div.tab").index(e.target);
			$('table.ratings tbody').hide(0);
			$('table.ratings tbody:eq('+tbodyindex+')').show(0);
		}
	});
	

/* 
 * ----------------------------------------------------------------------------- 
 * belso linkelos select
 * -----------------------------------------------------------------------------
 */
	
	$('#innernavigate').change(
		function(){
			document.location.href=$(this).find('option:selected').val();
		}
	);

/* 
 * ----------------------------------------------------------------------------- 
 * search box enter
 * -----------------------------------------------------------------------------
 */
 
 	$('div#search input.text').keypress( function(event) {
 		if(event.keyCode==13) {
 			$('#topsearch').submit();
 		}
 	});


/* 
 * ----------------------------------------------------------------------------- 
 * selected fx news also divjenek bordere leszed
 * -----------------------------------------------------------------------------
 */
 
 	$('#selectedfxnews div:last').css('border','0px');

	
/* 
 * ----------------------------------------------------------------------------- 
 * ie hack
 * -----------------------------------------------------------------------------
 */
	if($.browser.msie) {
		if($.browser.version=='6.0') {
			/* XHTML <?xml.. box layout bugfix */
			$('div').each(function(){
				if($(this).css('width')!='auto') {
					$(this).css('width',parseInt($(this).css('width')) + parseInt($(this).css('border-left-width')) + parseInt($(this).css('border-right-width')) + parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right')));
				}
			});

			/* hover on table rows */
			$('tr').each(function(){
				$(this).hover(
					function(){
						$(this).find('td').addClass('hover');
					},
					function(){
						$(this).find('td').removeClass('hover');
					}
				);
			});

		}
	}


/* 
 * ----------------------------------------------------------------------------- 
 * load external content, show frame
 * -----------------------------------------------------------------------------
 */
	$("#forexnewsbox").attr("src","http://www.forextv.com/Forex/News/ShowTitlesIFrame-Local.jsp?cat=1");
	if($('#right').height() > $('#left').height()) {
		$('#left').height($('#right').height()-8);
	}




/* 
 * ----------------------------------------------------------------------------- 
 * ajax error handler
 * -----------------------------------------------------------------------------
 */
	$('div#frame').ajaxError(function(request, settings){
		/*showError('<b>'+settings.status+' - '+settings.statusText+"</b><br />An error occured while loading the page, please try again later...<br />");*/
 	});
 	
 	
 	
});

function showError(msg) {
	var closeButton=$('<div class="closebtn">Close</div>');
	$('object,select').css('visibility','hidden');
	$('body').css('overflow','hidden');
	$('<div id="errormsg"></div>').appendTo("body");
	$('<div id="errormsg_bg"></div>').appendTo("body");
	
	$('#errormsg_bg').css({width: $(document).width(), height: $(document).height()});
	$('#errormsg').html(msg).click(
		function(){
			$('body').css('overflow','auto');
			$('object,select').css('visibility','visible');
			$('#errormsg,#errormsg_bg').remove();
		}
	).append(closeButton);
}

function openPopup(URL,windowName,width,height,vscroll) {
			var popWidth   =  width || 800; 
			var popHeight  =  height || 600; 
			var popLeft    =  (screen.width  - popWidth)  /2; 
			var popTop     =  (screen.height - popHeight) /2; 
			window.open(URL,windowName,'width='+popWidth+',height='+popHeight+',left='+popLeft+',top='+popTop+',scrollbars='+vscroll + ',resizable=1');
}