// check if we are in Mini's edit interface. If not, load the site script
if (jq14('#EditingContents').length == 0) {

/* date format converter on listing pages */
jq14('#foi-listing li p.date-published,  #news-media-listing li p.date-published, #newsRelease-date').each(function(){

	if ( jq14(this).html().length !== 0) {

		var formatedDate = jq14(this).html().split(/T/)[0].split(/-/);
		var documentDate = new Date(formatedDate[0],formatedDate[1]-1,formatedDate[2]);
		var month = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
		var documentDateDay = documentDate.getDate();
		var documentDateMonth = month[documentDate.getMonth()];
		var documentDateYear = documentDate.getFullYear();
		jq14(this).html(documentDateDay + ' ' +  documentDateMonth + ' ' + documentDateYear);
	}
});


/* collapsible text feature */
jq14('.toggle-section').each(
	function () {
		// initially hide the extra content
		jq14(this).hide();
		
		// if the title element is present, use it as the show extra content text link 
		if (jq14(this).attr('title').length === 0) {
			jq14(this).before('<p class="toggle-section-more">read more<\/p>');
		}
		else {
			jq14(this).before('<p class="toggle-section-more">' + jq14(this).attr('title') + '<\/p>');
		}
		
		// when clicking the extra content link, display or hide the relevant content
		jq14('.toggle-section-more').toggle(
			function () {
				jq14(this).html('hide this section');
				jq14(this).next('.toggle-section').slideDown();
			},
			function () {
				if (jq14(this).next('.toggle-section').attr('title').length === 0) {
					jq14(this).html('read more');
				}
				else {
					jq14(this).html(jq14(this).next('.toggle-section').attr('title'));
				}
				jq14(this).next('.toggle-section').slideUp();
			}
		);
	}
);



/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupFormStatus = 0;

//loading popup with jq14 magic!
function loadPopupForm(){
	//loads popup only if it is disabled
	if(popupFormStatus==0){
		jq14("#backgroundPopup").css({
			"opacity": "0.7"
		});
		jq14("#backgroundPopup").fadeIn("slow");
		jq14("#popupContact").fadeIn("slow");
		popupFormStatus = 1;
	}
}

//disabling popup with jq14 magic!
function disablePopupForm(){
	//disables popup only if it is enabled
	if(popupFormStatus==1){
		jq14("#backgroundPopup").fadeOut("slow");
		jq14("#popupContact").fadeOut("slow");
		popupFormStatus = 0;
	}
}

//centering popup
function centerPopupForm(){
	//request data for centering
	var formWindowWidth = document.documentElement.clientWidth;
	var formWindowHeight = document.documentElement.clientHeight;
	var formPopupHeight = jq14("#popupContact").height();
	var formPopupWidth = jq14("#popupContact").width();
	//centering
	jq14("#popupContact").css({
		"position": "absolute",
		"top": formWindowHeight/2-formPopupHeight/2,
		"left": formWindowWidth/2-formPopupWidth/2
	});
	//only need force for IE6
	
	jq14("#backgroundPopup").css({
		"height": formWindowHeight
	});
}

function fillPopUpFormContent() {
	//empty content
	jq14('#popupContact ul').empty();
	//fill the pop up container with informnation from the form
	jq14('#form207 .form-list .form-item, #form183 .form-list .form-item, #form212 .form-list .form-item').each(function() {
	    if ( jq14(this).children('label').text() ) {
	        var getLabelValue = jq14(this).children('label').text();
	        jq14('#popupContact ul').append('<li class="popupFormQuestion">' + getLabelValue + ':</li>');
	    }
	    if( jq14(this).find('.form-question-textarea').length !== 0 ) {
	    	var getTextAreaValue = jq14(this).find('.form-question-textarea').val();
	        jq14('#popupContact ul').append('<li class="popupFormAnswer">' + getTextAreaValue + '</li>');
	    }
	    else if ( jq14(this).find('.form-question-text').length !== 0 ) {
	    	var getInputValue = jq14(this).find('.form-question-text').val();
	        jq14('#popupContact ul').append('<li class="popupFormAnswer">' + getInputValue + '</li>');
	    }
	    else if ( jq14(this).find('.form-question-select').length !== 0 ) {
	    	var getSelectValue = jq14(this).find('.form-question-select :selected').text();
	        jq14('#popupContact ul').append('<li class="popupFormAnswer">' + getSelectValue + '</li>');
	    }
	});
}

//create the preview link for the selected forms. this needs to be edited if this feature is need on other forms
jq14('#form207-questions .form-item .form-item-button, #form183-questions .form-item .form-item-button, #form212-questions .form-item .form-item-button').parent('.form-item')
.before('<a href="#" id="clickPreview">Click here to preview<\/a>');

// create the pop up container and background container elements 
jq14('body').append('<div id="backgroundPopup"><\/div>');
jq14('body').append('<div id="popupContact"><h1>Preview<\/h1><ul><\/ul><a id="popupContactClose">Close preview</a><\/div>');



//LOADING POPUP
//Click the button event!
jq14("#clickPreview").click(function(){
	//call the function to fill/update the content for the popup
	fillPopUpFormContent();
	//centering with css
	centerPopupForm();
	//load popup
	loadPopupForm();
});
			
//CLOSING POPUP
//Click the x event!
jq14("#popupContactClose").click(function(){
	disablePopupForm();
});
//Click out event!
jq14("#backgroundPopup").click(function(){
	disablePopupForm();
});
//Press Escape event!
jq14(document).keypress(function(e){
	if(e.keyCode==27 && popupFormStatus==1){
		disablePopupForm();
	}
});


/* open pdf documents in new window */
jq14('#content a').each(function() {

	if (jq14(this).attr('href') !== undefined){
		var linkUrl = jq14(this).attr('href');
		//console.log(linkUrl);
		if (linkUrl.search('.pdf') != -1) {
			jq14(this).attr('target','_blank');
		}
	}
	
});

}

