var isOpen=false;
var viewPages=new Array();

function initPage(){
	makeDropdown(); 																//makes the dropdown for selecting the page
	makeSubscribe();																//makes the subscribe form
	showGalleryIndex();																//shows the 'Y of X' next to the media galleries
	//SUBSCRIPTION EDIT: validation();																	//sets the validation for the newsletter form
	
	//$("#pageFilter").val($('#currentDisplay').text().toLowerCase());				//sets the current page to the one selected by php $_GET['page'] from the #currentDisplay field 
	switchContent($('#currentDisplay').text().toLowerCase()); 						//set content to current page
	
	setTimeout( function(){															//scroll to item given by php $_GET['item'] from the #currentItem field
							$.scrollTo($('#newsItem'+$("#currentItem").html()), 1000, {easing:'easeInOutExpo'});
							}, 200);
	
	setTimeout ( 'hideError()', 5000 ); 											//hide error messages after 5 seconds
	
	jQuery.validator.addMethod("sennepType", function(value, element) {				//add validation type for letters (upper/lower case, accented or not) and spaces only
		return this.optional(element) || /^[a-zA-ZĄ-’ ]+$/i.test(value);
	}, ""); 
	
	if(navigator.appName=='Microsoft Internet Explorer')	{						//change size of subscribe form if user is browsing with IE (why would you do that anyway?)
		$('#newsletter_form').css("width","998px");
	}
}


function validation(){
	// validate signup form on keyup and submit
	$("#newsletterSubscribeForm").validate({
		 highlight: function(element, errorClass) {
			 $(element).css("background-image","url(images/input_field_invalid.gif)")
		 },
		 unhighlight: function(element, errorClass) {
			 $(element).css("background-image","url(images/input_field.gif)")
		 },
		//sets all error messages to empty
		messages: {
			firstname:{
				required: ""
			},
			surname:{
				required: ""
			},
			email:{
				required: "",
				email: ""
			}
		},
		submitHandler: function(form) {
			var firstname		= $('#firstname').attr('value');
			var surname			= $('#surname').attr('value'); 
			var email			= $('#email').attr('value'); 
			var profession		= $('#profession').attr('value'); 
			var country			= $('#country').attr('value'); 
	
			$.ajax({
				type: "POST",
				url: "http://www.sennep.com/mailer/form.php?form=1",
				data: "firstname="+ firstname +"& surname="+ surname +"& email="+ email +"& profession="+ profession +"& country="+ country +"& submitNewsletterSubmit='submitted'",
				success: submitHandler()
			});
			return false;
		 }
	});	
}


//handles submitting the form, will empty all the fields and hide it
function submitHandler(){
	$('#firstname').removeAttr('value') ;
	$('#surname').removeAttr('value') ;
	$('#email').removeAttr('value') ;
	$('#profession').removeAttr('value') ;
	$('#country').removeAttr('value') ;
	toggle_slider();
	
	return false;
}

//shows the '1 of 7' next to the media galleries
function showGalleryIndex(){
	$('.mediaIdent').show();
}

//makes the subscribe form button needed
function makeSubscribe(){
	//$('#handyLinks').prepend('<a href="javascript:void(0);" onClick="toggle_slider();">Subscribe newsletter</a><br/>');
	//added in index.php instead
}

//fades the error message away
function hideError(){
	$('#errorMessage').fadeOut(2000);
}

//opens and closes the newsletter submit form
function toggle_slider(){
	if(!isOpen){
		$('#vertical_slider').animate({ height:"458px"}, 1000, "easeInOutExpo");
		$('#newsletter_form').animate({ top : "0px"}, 1000, "easeInOutExpo");
		$('#Sennep_logo').animate({ top : "488px"}, 1000, "easeInOutExpo");
		isOpen=true;
	}
	else if(isOpen){
		$('#vertical_slider').animate({ height:"0px" }, 1000, "easeInOutExpo");
		$('#newsletter_form').animate({ top : "-458px"}, 1000,"easeInOutExpo" );
		$('#Sennep_logo').animate({ top : "30px"}, 1000, "easeInOutExpo");
		isOpen=false;
	}
	return false;
}


//changes the content on the page matching the selected page from the dropdown
function switchContent( idClicked ){
	var gotoId = idClicked.replace("link", "");
	var elements=$('.Subcontainer');
	var elementsVisibility=new Array();
	
	//hide all items
	for (i=0;i<elements.length;i++){
		elementsVisibility.push(true);
		//if(	$('#pageFilter option:selected').val()!=elements[i].id.toLowerCase())
		if(	gotoId.toLowerCase() != elements[i].id.toLowerCase())
			elementsVisibility[i]=false;
	}
	
	for (i=0;i<elements.length;i++)
	{	
		if(elementsVisibility[i]) {
			$('#'+elements[i].id).fadeIn(500);
			$('#'+elements[i].id+'link').addClass('selected');
			pageViewed = false;
			for (j=0;j<viewPages.length;j++) {
				if (i == viewPages[j]) pageViewed = true;
			}
			if (!pageViewed) {
				setTimeout ( "replaceFont()", 100 );
				viewPages.push(i);
			}
		}
		else {
			$('#'+elements[i].id).fadeOut(10);
			$('#'+elements[i].id+'link').removeClass('selected');
		}
	}

	//show selected item

	//SUBSCRIPTION EDIT: $('#newsletterSubscribeForm').attr("action", "?page="+($('#pageFilter option:selected').val().toLowerCase())); 

return false;
}

function replaceFont(){
	sIFR.replace(sennep, {
	  selector: '.sectionheader h2.big',
	  tuneHeight: '-5',
	  ratios: [8, 1.3, 11, 1.21, 12, 1.2, 18, 1.19, 23, 1.15, 25, 1.14, 35, 1.13, 45, 1.12, 46, 1.11, 47, 1.12, 62, 1.11, 63, 1.1, 68, 1.11, 69, 1.1, 71, 1.11, 1.1]
	});
	sIFR.replace(sennepsmall, {
	  selector: '.sectionheader h2.top',
	  ratios: [8, 1.3, 11, 1.21, 12, 1.2, 18, 1.19, 23, 1.15, 25, 1.14, 35, 1.13, 45, 1.12, 46, 1.11, 47, 1.12, 62, 1.11, 63, 1.1, 68, 1.11, 69, 1.1, 71, 1.11, 1.1]
	});
}

//makes the dropdown for selecting the content
function makeDropdown(){			
	/*var elements=$('.Subcontainer');
	var Options=" ";
	
	$('#Sennep_logo').append('<select id="pageFilter" style="float:right;cursor:pointer; width:160px;" onChange="switchContent();"></select>');
	
	for (i=0;i<elements.length;i++){
		$('#pageFilter').append(' <option value="'+elements[i].id.toLowerCase()+'">'+elements[i].id.split('_').join(' ')+'</option>');
	}*/
	//Link list added in index.php instead
}