var signpainter = {
	src: '/js/sifr/signpainter.swf',
	wmode: 'transparent'
};
sIFR.activate(signpainter);

$(document).ready(function(){
	for(var i=1;i<5;i++)
	{
		$('#menu'+i).mouseover(function(){
			var number = $(this).attr('id').replace('menu','');
			$(this).attr('src','/images/menu'+number+'_over.gif');
		});
		
		$('#menu'+i).mouseout(function(){
			var number = $(this).attr('id').replace('menu','');
			if(!$(this).parent().parent().hasClass('selected'))
			{
				$(this).attr('src','/images/menu'+number+'.gif');
			}
		});
	}
	
	sIFR.replace(signpainter, {
	    selector: '#ingman p'
	    ,forceSingleLine: true
	    ,offsetLeft: '5px'
	    ,tuneWidth: '10px'
	    ,css: [
	      '.sIFR-root { font-size:26px; color:#ffffff;}'
	       ,'a { text-decoration: none; color:#ffffff;}'
	       ,'a:link { color: #ffffff; }'
	       ,'a:hover { color: #ffffff; text-decoration: underline;}'
	    ]
	});
	
	sIFR.replace(signpainter, {
	    selector: '#stats_container h1, h1.grey'
	    ,forceSingleLine: true
	    ,offsetLeft: '5px'
	    ,tuneWidth: '10px'
	    ,css: [
	      '.sIFR-root { font-size:37px; color:#727272;}'
	    ]
	});
	
		
	
	
	sIFR.replace(signpainter, {
	    selector: 'h1'
	    ,forceSingleLine: true
	    ,offsetLeft: '5px'
	    ,tuneWidth: '10px'
	    ,css: [
	      '.sIFR-root { font-size:26px; color:#6a0039;}'
	    ]
	});
	sIFR.replace(signpainter, {
	    selector: '#stats_container h2'
	    ,forceSingleLine: true
	    ,offsetLeft: '5px'
	    ,tuneWidth: '10px'
	    ,css: [
	      '.sIFR-root { font-size:23px; color:#cb006d;}'
	    ]
	});
	
	if($('.taste_container').length>0)
	{
		$('.taste_container').mouseover(function(){
			var idNum = $("img",this).attr('src').replace(/\/images\/taste/,'');
			idNum=idNum.replace(/.jpg/,'');
			$(this).prepend('<div class="taste_choose"><a href="javascript:selectTaste('+idNum+');"><img src="/images/taste_select.png" alt="Valitse" border="0" width="256" height="66" /></a></div>');
			$('.taste_choose').mouseout(function(){				
				$('.taste_choose').remove();
			});
		});
		
		
	}
});

function closeDialogs()
{
	$('#rules_container, #video_container, #snitch_badge_container, #bigheart_badge_container').hide();
}

function closeBook()
{
	$("#book_container").hide();
}

function closeSummary()
{
	$("#final_stage_container").hide();
	$("#campaignlink_container").show();
}
function showSummary()
{
	$("#final_stage_container").show();
	$("#campaignlink_container").hide();
	sIFR.replace(signpainter, {
	    selector: '#final_stage_container h1'
	    ,offsetLeft: '5px'
	    ,tuneWidth: '10px'
	    ,css: [
	      '.sIFR-root { font-size:27px; color:#141414;text-align:center;}'
	    ]
	});
	sIFR.replace(signpainter, {
	    selector: '#final_stage_container h2'
	    ,forceSingleLine: true
	    ,offsetLeft: '5px'
	    ,tuneWidth: '10px'
	    ,css: [
	      '.sIFR-root { font-size:23px; color:#cb006d;}'
	    ]
	});
}
function showBook(page,order_type,hash)
{
	$.ajax({
		type: "post",
		url: "/kirja", 
		data: {'page': page,'order_type': order_type,'hash':hash},
		success: function(data){
			closeDialogs();
			$("#book_container").html(data);
			$("#book_container").show();
			sIFR.replace(signpainter, {
			    selector: 'h1'
			    ,forceSingleLine: true
			    ,offsetLeft: '5px'
			    ,tuneWidth: '10px'
			    ,css: [
			      '.sIFR-root { font-size:26px; color:#6a0039;}'
			    ]
			});
	 }});
}

function showRegister()
{
	$.ajax({
		type: "post",
		url: "/ilmianna", 
		success: function(data){
			$("#book_container").html(data);
			$("#book_container").show();
			sIFR.replace(signpainter, {
			    selector: 'h1'
			    ,forceSingleLine: true
			    ,offsetLeft: '5px'
			    ,tuneWidth: '10px'
			    ,css: [
			      '.sIFR-root { font-size:26px; color:#6a0039;}'
			    ]
			});
			var options = {
				'maxCharacterSize': 200,
				'displayFormat': '#input / #max merkkiä'
			};
			$('#coworker_arguments').textareaCount(options);
			
			options.displayFormat='';
			options.maxCharacterSize=60;
			$('#informer_name').textareaCount(options);
			
			options.maxCharacterSize=150;
			$('#informer_email').textareaCount(options);
			
			options.maxCharacterSize=50;
			$('#informer_phone').textareaCount(options);
			$('#coworker_name').textareaCount(options);
			
			options.maxCharacterSize=100;
			$('#coworker_job').textareaCount(options);
	 }});
}

function sendRegister()
{
	var status=true;
	var requiredFields = new Array('informer_name','informer_phone','coworker_name','coworker_job','coworker_arguments');
	
	for(var i=0;i<requiredFields.length;i++)
	{
		if($("#" + requiredFields[i]).val()=='')
		{
			$('label[for="'+requiredFields[i]+'"]').css('color','red');
			status=false;
		}
		else
		{
			$('label[for="'+requiredFields[i]+'"]').css('color','#353535');
		}
	}
	
	if(!checkEmail($('#informer_email').val()))
	{
		$('label[for="informer_email"]').css('color','red');
		status=false;
	}
	else
	{
		$('label[for="informer_email"]').css('color','#353535');
	}		
	
	if($('#coworker_city_id').val()==0)
	{
		$('label[for="coworker_city_id"]').css('color','red');
		status=false;
	}
	else
	{
		$('label[for="coworker_city_id"]').css('color','#353535');
	}
	
	if($('input[name="coworker_gender"]:checked').length==0)
	{
		$('label[for="coworker_gender"]').css('color','red');
		status=false;
	}
	else
	{
		$('label[for="coworker_gender"]').css('color','#353535');
	}
	
	if($('#policy_accepted:checked').length==0)
	{
		$('label[for="policy_accepted"]').css('color','red');
		status=false;
	}
	else
	{
		$('label[for="policy_accepted"]').css('color','#353535');
	}
	
	if(status)
	{
		$('#send_register').attr('href','#');
		$.ajax({
			type: "post",
			url: "/tallenna-ilmianto", 
			data: $("#register").serialize(),
			success: function(data){
				if(data==0)
				{
					alert('Tapahtui jokin virhe, yritä uudestaan, kiitos!');
					$('#send_register').attr('href','javascript:sendRegister();');
				}
				else
				{
					$(':input','#register')
					 .not(':button, :submit, :reset, :hidden')
					 .val('')
					 .removeAttr('checked')
					 .removeAttr('selected');
					
					$('#thanks_page').html(data);
					$('#thanks_page').show();
					sIFR.replace(signpainter, {
					    selector: '#thanks_page h1'
					    ,forceSingleLine: true
					    ,offsetLeft: '5px'
					    ,tuneWidth: '10px'
					    ,css: [
					      '.sIFR-root { font-size:28px; color:#6a0039;}'
					    ]
					});
					sIFR.replace(signpainter, {
					    selector: '#thanks_page h2'
					    ,forceSingleLine: true
					    ,offsetLeft: '5px'
					    ,tuneWidth: '10px'
					    ,tuneHeight: '10px'
					    ,offsetTop:  '10px'
					    ,css: [
					      '.sIFR-root { font-size:19px;color:#6a0039;}'
					    ]
					});
				}
		 }});
	}
}

function closeThanks(hash)
{
	$('#thanks_page').hide();
	showBook(1,1,hash);
}

function shareLink(urli)
{
	window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(urli),'facebookShare','width=626, height=436, status=0, toolbar=0');
}

function selectTaste(idNum)
{
	$.ajax({
		type: "post",
		url: "/makuvalinta", 
		data: {taste_num: idNum},
		success: function(data){
			$("#taste_results").html(data);
			$("#taste_results").show();
			
			$('.taste_container, .taste_choose').unbind();
			$('.taste_choose').remove();
		}
	 });
}

function showVideo()
{
	$('#video_container').show();
	var flashVars = {
		src: "http://www.kingis.fi/flash/kingis.f4v",
		autoPlay: true
	};
	var params = {
			allowFullScreen: true
	};

	swfobject.embedSWF("/flash/StrobeMediaPlayback.swf", "flash_container", "655", "360", "10.0.0", "/flash/expressInstall.swf",flashVars,params);
	swfobject.createCSS("#flash_container","outline:none");
}

function closeVideo()
{
	$('#video_container').hide();
	$('#video_container').html('<div class="close_btn"><a href="javascript:closeVideo();">Sulje (x)</a></div><div class="clearer">&nbsp;</div><div id="flash_container"><h1>Tarvitset vähintään Flash 10.0-liitännäisen katsoaksesi videon.</h1><p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div>');
}

function showRules()
{
	$('#rules_container').show();
	sIFR.replace(signpainter, {
	    selector: '#rules_container h1'
	    ,forceSingleLine: true
	    ,offsetLeft: '5px'
	    ,tuneWidth: '10px'
	    ,css: [
	      '.sIFR-root { font-size:26px; color:#6a0039;}'
	    ]
	});
}

function closeRules()
{
	$('#rules_container').hide();
}

function showBadgeDialog(idName)
{
	$('#'+idName).show();
	sIFR.replace(signpainter, {
	    selector: '.dialog_small h1'
	    ,forceSingleLine: true
	    ,offsetLeft: '5px'
	    ,tuneWidth: '10px'
	    ,css: [
	      '.sIFR-root { font-size:26px; color:#6a0039;}'
	    ]
	});
}
function closeBadgeDialog(idName)
{
	$('#'+idName).hide();
}

function checkEmail(emailStr)
{
	var status=true;
	var email_pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	if(!email_pattern.test(emailStr))
	{
		status = false;
	}
	
	return status;
}

