var msie = document.all && (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
var msie6 = document.all && (navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1);
var msie7 = document.all && (navigator.userAgent.toLowerCase().indexOf("msie 7.") != -1);

$(document).ready(function(){
	$('img.print').click(function(){
		window.print();
	});
	
	//valtozokor form
	if ($('table.valtozo').length>0){
		$('button.valtozo').click(function(){
			
			var eletminoseg = 0;
			var tunetek = 0;
			
			$('table.valtozo input[name^=question]').each(function(){
				if ($(this).is(':checked')){
					eletminoseg += parseInt($(this).val());
				}
				
			});
			$('table.valtozo input[name^=point]').each(function(){
				if ($(this).is(':checked')){
					tunetek += parseInt($(this).val());
				}
				
			});
			$('span.eletminoseg span').html(eletminoseg);
			$('span.pontszam span').html(tunetek);
			
			
			$('div.valtozoResult').slideDown(100,function(){
				$.scrollTo('div.valtozoResult',100);		
			});
			
			
		});
	}
	
});

//peti orvoskereso script
function setCityselect() {
	$.getJSON('/postalcodesearch/county_id/'+$('#county_id').val(), 
		function(data) {
			var opt = '<option value="0">---</option>';
			$.each(data, function(i,item){
				selected = '';
				if(i==selected_city_id) selected = ' selected' 
				opt = opt+'<option value="'+i+'"'+selected+'>'+item+'</option>';
			});
			$('#city_id').html(opt);
		}
	);
		
	if($('#county_id').val()!=20) {
		$('#city_select_holder').css( {display: 'block'} );
		$('#district_select_holder').css( {display: 'none'} );
	} else {
		$('#city_select_holder').css( {display: 'none'} );
		$('#district_select_holder').css( {display: 'block'} );
		$('#city_id').val(0);
	}
}

function setThemeselect() {
	
	if ( window.select_theme_text == undefined) {
		return true;
	}
	
	$.getJSON('/getthemeoptions/category_id/'+$('#category_id').val(), 
		function(data) {
			var opt = '<option value="0">'+select_theme_text+'</option>';
			$.each(data, function(i,item){
				selected = '';
				if(i==selected_theme_id) selected = ' selected' 
				opt = opt+'<option value="'+i+'"'+selected+'>'+item+'</option>';
			});
			$('#theme_id').html(opt);
		}
	);
}

function showPostanswerForm(post_id) {
	if ( $('#postanswer_'+post_id).css('display') == 'none' ) {
		$('#postanswer_'+post_id).css( { display: 'block' } );
	} else {
		$('#postanswer_'+post_id).css( { display: 'none' } );
	}
	
}

function showNewthemeForm() {
	if ( $('#newtheme').css('display') == 'none' ) {
		$('#newtheme').css( {display: 'block'} );
	} else {
		$('#newtheme').css( {display: 'none'} );
	}
}
function showNewpostForm() {
	if ( $('#newpost').css('display') == 'none' ) {
		$('#newpost').css( {display: 'block'} );
	} else {
		$('#newpost').css( {display: 'none'} );
	}
}

$(function() {
	
	setCityselect();
	setThemeselect();
	
	
	//Login label
	$('#Login div.inputbg input').each(function(i) {
		var field = this; 
		var labelin = $('#Login label').eq(i).removeClass('screenrdr').addClass('labelin').css({display: (this.value.length == 0) ? 'block' : 'none', left: $(this).position().left, top: $(this).position().top + 2});
		$(this)[0].labelin = labelin;
		$(labelin).click(function() {field.focus();});
		$(labelin).focus(function() {field.focus();});
		$(field).focus(function() {$(labelin).css({display: 'none'});});
		$(field).blur(function() {$(labelin).css({display: (this.value.length == 0) ? 'block' : 'none'});});
		$(this).parent().append(labelin);
	});
	
	// kalkulatorok flash
	var flashParams = { 'wmode' : 'transparent' };
	if ( $("#bmicalc").length ) {
		swfobject.embedSWF( 
			'swf/richter_intimzona_bmi_kalkulator.swf',
			"bmicalc", "520", "520", "9.0.0", 'swf/expressInstall.swf', flashVars, flashParams
		);
	}
	if ( $("#terhessegcalc").length ) {
		swfobject.embedSWF( 
			'swf/richter_intimzona_terhessegi_korong.swf',
			"terhessegcalc", "520", "520", "9.0.0", 'swf/expressInstall.swf', flashVars, flashParams
		);
	}
	
	if ( $("#ovulaciocalc").length ) {
		swfobject.embedSWF( 
			'swf/richter_intimzona_ovulacio.swf',
			"ovulaciocalc", "520", "520", "9.0.0", 'swf/expressInstall.swf', flashVars, flashParams
		);
	}
	

	var flashVars = {'selectedButtonId' : $('#SelectedMenu').html(), 'loggedin': ($('#Login').hasClass('loggedin')) ? 1 : 0};
	var flashParams = { 'base' : 'swf/', 'wmode' : 'transparent' };

	swfobject.embedSWF( 
		'swf/richter_intimzona_preloader.swf',
		"HeadFlash", "990", "300", "9.0.0", 'swf/expressInstall.swf', flashVars, flashParams
	);
	

	flashVars = {
		'autostart': 'false',
		'frontcolor': 'BE739F',
		'lightcolor': '621643',
		'stretching': 'fill',
		'controlbar': 'bottom'
	};
	flashParams = {};
	
	// Videok
	$('ul.videolist li').each(function(i) {
		$(this).find('a').click(function(e) {
			e.preventDefault();
			
			var $closeLi = $(this).closest('li');
			
			$closeLi
				.parent().find('span.play')
					.css({display: 'block'})
				.end().end()
				.find('span.play')
					.css({display: 'none'})
				.end();
			
			$(".videoplayerwrp").html('<div id="videoplayer"></div>');
			flashVars.file = $closeLi.find('span.flvurl').html();
			swfobject.embedSWF( 
				'swf/jw_player.swf',
				"videoplayer", "308", "271", "9.0.0", 'swf/expressInstall.swf', flashVars, flashParams
			);
			
			$('.videoplayerwrp').parent().find('h3').html( $closeLi.find('h4 a').html() );
			$('.videoplayerwrp').parent().find('p').html( $closeLi.find('span.desc').html() );
		});
	});
	
	// Audio
	$('div.audioplayerwrp').each(function(i) {
		var $playerDiv = $(this).find('div');
		
		flashVars.file = $playerDiv.html();
		swfobject.embedSWF( 
			'swf/jw_player.swf',
			$playerDiv.attr('id'), "270", "20", "9.0.0", 'swf/expressInstall.swf', flashVars, flashParams
		);
	});	
	$('ul.videolist li:first a:eq(0)').click();
	
	if ( ! msie ) {
		//IE alatt kulonben nincs kep, csak hang
		flashVars.autostart = true;
	}
	
	//cufon replace
	Cufon.replace('.cfn_replace');
	Cufon.replace('.tabwrp .c', { separate: 'none' });
	Cufon.now();



	//Tabos menu megfelelo sortoresei
	if ( $('ul.tabmenu li').length ) {
		
		var rowSize = [];
		var e = 0;
		var rowBreakIndex = 0;
		
		$('ul.tabmenu li').each(function(i) {
			
			if ( isNaN( rowSize[e] ) ) {
				rowSize[e] = 0;
			}
			
//			alert( i + ': ' + rowSize[e] + ' => ' + $(this).outerWidth() );
			if ( rowSize[e] + $(this).outerWidth() > 612  ) {
				rowSize[ e+1 ] = 0;
				rowSize[ e+1 ] += $(this).outerWidth();
				rowBreakIndex = i;
				e++;
			} else {
				rowSize[e] += $(this).outerWidth();
			}
			
		});
		
		
		// egyelore csak a ketsoros menure figyelunk.. :(
			
		//alert( rowSize[0] + " " + rowSize[1] );
		if ( e > 0 && rowSize[0] > rowSize[1] ) {
			if ( msie6 || msie7) {
				$('ul.tabmenu li:lt(' + (rowBreakIndex-1) + ')').wrapAll('<ul class="clr" style="clear: both"></ul>');
				$('ul.tabmenu li:gt(' + (rowBreakIndex-2) + ')').wrapAll('<ul class="clr" style="clear: both"></ul>');
			} else {
				$('ul.tabmenu li').eq( rowBreakIndex-1 ).css({clear: 'left'});				
			}
			
		} else if ( e > 0 ) {
			
			if ( msie6 || msie7 ) {
				$('ul.tabmenu li:lt(' + (rowBreakIndex) + ')').wrapAll('<ul class="clr"></ul>');
				$('ul.tabmenu li:gt(' + (rowBreakIndex-1) + ')').wrapAll('<ul class="clr"></ul>');
			} else {
				$('ul.tabmenu li').eq( rowBreakIndex ).css({clear: 'left'});
			}
		}
			
	}
	


});
