  // You may specify partial version numbers, such as "1" or "1.3",
  //  with the same result. Doing so will automatically load the 
  //  latest version matching that partial revision pattern 
  //  (e.g. 1.3 would load 1.3.2 today and 1 would load 1.4.1).
  google.load("jquery", "1");
  google.load("jqueryui", "1.8.0");
  
  google.setOnLoadCallback(function() {
	  	if( window.location.hash.substring(0,6) == '#page-' ){
	  		var href = location.href.split("#");
	  		var pagenr = href[1].substring(5);
	  		$("#ads").load( href[0]+"/"+pagenr + " #ads");
	  	}
	  
	  	$(".choose-color").click( function(){
			$("#input-background").val('/images/'+$(this).attr("id")+'.png');
			getImage();
		});

		/* Show the next slide of ads */
		$("#ads .buttons a").live('click', function(event){
			event.preventDefault();
			var href = $(this).attr("href");
			var path = href.split("/");
			var height = $("#background").height();
			
			$("#background").css("height", height+"px");
			if( path[3] ){
				location.hash = "page-"+path[3];
			}else{
				location.hash = "page-"+path[2];
			}
//			$(".buttons .button-nasta").attr("href", path[0] + "/" + path[1] + "/" + path[2] + "/" + ( parseInt( path[3] )+1 ) );
//			$(".buttons .button-faregaende").attr("href", path[0] + "/" + path[1] + "/" + path[2] + "/" + ( parseInt( path[3] )-1 ) );
			$("#ads").hide('slide', {direction: 'left'}, 500);
			$("#ads").load( href + " #ads", function(){
				$("#ads").show('slide', {direction: 'right'}, 500);
			});
		});
		
		$("a.popup").live("click", function(event){
			event.preventDefault();
			$("body").append( $("<div id='popup-bg'></div><div id='popup-content'></div>") );
			$("#popup-bg").css("height", $("body").height()+"px");
			$("#popup-content").load( $(this).attr("href") + " #content", function() {
				sIFR.replace(timesbolditalic, {
					selector: 'h1,h2,h3,h4',
					wmode: 'transparent',
					css: ['.sIFR-root { font-family: "times"; font-weight:bold; }',
					      'a { color:#000000; text-decoration:none; }'],
					tuneHeight: -2,
					fitExactly: true
				});				
			});
		});
		
		$("#popup-bg").live("click", function(event){
			$("#popup-bg").remove();
			$("#popup-content").remove();
		});
		
		$("#vote-button").live('click', function(event){
/*			event.preventDefault();
			
			var elements = $("#vote-form").serializeArray();
			var voteelement = $(this).parent("ul").children(".votes");
			var votes = voteelement.text();
			
			$("#votemess").load( location.href + " #votemess", elements, function(){
				voteelement.text( parseInt( votes ) + 1 );
			});*/
		});
		
		$("#img-create form input[type=submit]").live('click', function(event){
			event.preventDefault();
			var elements = $("#img-create form").serializeArray();
			
			var name = '';
			var val = '';
			var errorText = '';
			
			for(i=0;i<elements.length;i++){
				name = $( elements[i] ).attr("name");
				val = $(elements[i]).val();

				switch( name ){
				case 'program':
					if( val == '' || val == 'Eller hitta på ett nytt!' ){
						errorText = 'Välj ett program';
					}
					break;
				case 'text':
					if( val == '' || val == 'Skriv din text här...'){
						errorText = 'Du måste skriva i en text';
					}
					break;
				case 'name':
					if( val == '' || val == 'För- och efternamn...'){
						errorText = 'Fyll i ditt namn';
					}
					break;
				case 'email':
					if( val == '' ){
						errorText = 'Fyll i din e-postadress';
					}
					break;
				}
			}
			
			if( $("#confirm-1").length != 0 ){
			
				if( $("#confirm-1").is(':checked') == false ){
					errorText = 'Du måste godkänna villkoren.';
				}
				
			}
			
			if( $("#confirm-2").length != 0 ){
			
				if( $("#confirm-2").is(':checked') == false ){
					errorText = 'Du måste godkänna villkoren.';
				}
				
			}
			
			if( errorText != '' ){
				$("#error").text( errorText );
				$("#error").css("display","block")
				sIFR.replace(timesbolditalic, {
					selector: 'h3.red',
					wmode: 'transparent',
					css: ['.sIFR-root { font-family: "times"; font-weight:bold; color:#FFFFFF; } ',
					      'a { color:#FFFFFF; text-decoration:none; }',
					      'a:hover { color:#FFFFFF; text-decoration:none; }'],
					fitExactly: true
				});
				return;
			}
			
			createImgSubmit( elements );
			
//			return false;
		});

  });
  
	/* Preview image timeout */
  	var timer = null;
	function getImage(){
		if (timer) clearTimeout(timer);
		timer = setTimeout(getImageSubmit, 500);
	}

	/* Preview image */
	function getImageSubmit(){
		var background = $("#input-background").val();
		var text = $("#input-text").val();
		var program = $("#input-program").val();

		$.ajax({
			url: "/ajax/image.php",
			type: "POST",
			cache: false,
			data: "background="+background+"&text="+text+"&program="+program,
			success: function(data) {
				$("#input-file").val("/images/tmp/"+data+".png");
				$("#preview-container").html('<img src="/images/tmp/'+data+'.png" />');
			}	
		});
	}
	
	/* Save final image */
	function createImgSubmit( data ){
		var stepnr = $("#input-step").val();
		
		$("#step-"+ ( stepnr ) ).slideUp("slow");
		$("#img-create .left").load("/tavla #step-"+( parseInt(stepnr) + 1 ), data  , function(){
			sIFR.replace(timesbolditalic, {
				selector: 'h1,h2,h3,h4',
				wmode: 'transparent',
				css: ['.sIFR-root { font-family: "times"; font-weight:bold; } '],
				fitExactly: true
			});			
			$("#step-"+( stepnr + 1) ).slideDown("slow");
		});
	}

	/* Clear value on first focus */
	function clearField(thisa,defVal){
		 if(thisa.value==defVal){ thisa.value=''; }
		 $(thisa).css("color", "#000000");
	}

	/* Change the program in select list  */
	function changeProgram(){
		$("#input-program").css("color", "#000000");
		$("#input-program").val( $("#bladdra-program option:selected").text() );
		$("a.more").attr("href", $("#bladdra-program option:selected").val() )
		$("a.more").parent().css("display", "block");
		getImage();
	}
	
	/* Set max length in textarea */
	function ismaxlength(obj){
		var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
		if (obj.getAttribute && obj.value.length>mlength)
		obj.value=obj.value.substring(0,mlength)
	}
