$(document).ready(function(){
	alert("ada");
	// For aligned images
	$('img[align=right]').addClass('float-right');
	$('img[align=left]').addClass('float-left');	

	// Property module tabs	
	$("#property_tabs").tabs();	
	$("#tab_nav").show();	

	

	// Adds an icon and title for links that open in a new window
	$('p a[target=_blank]').attr('title', 'Opens in a new window').addClass('newindow');
	
	// Gallery Plugin
/*
	$("a[rel=image_gallery]").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'titlePosition' 	: 'inside',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	
	$("a.lightbox").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
*/

	// Quicklinks
	quicklinkgo();
	
	// Default slideshow - for opts see http://jquery.malsup.com/cycle/options.html
	$(".slider").v1Slider();
	
	// Documemt store slidedeck and form handler
	var $slideDeck = $('.slidedeck');
	if ($slideDeck[0])
		$slideDeck.slidedeck().vertical();
/* 	$("#docStoreSearch").v1DocStore(); */
	
	//Staff directory tree images
	var treeClosed = "base-install/images/staff-directory/arrow_blue_horiz.gif";
	var treeOpen = "base-install/images/staff-directory/arrow_blue_down.gif";
	
	///////////////// CMS specific code below /////////////////////
	
	// Search filter
	$(".searchAllModules").click(function (ev) {
		ev.preventDefault();
		var $this = $(this),
			check = true;
		if ($this.text() == "Select all") 
			$this.text("Deselect all");
		else {
			$this.text("Select all");
			check = false;
		}
		$this.nextAll("input[type=checkbox]").each(function () {
			if (check) this.checked = true;
			else this.checked = false;
		});
	}).nextAll("input[type=checkbox]").each( function () {
		if (this.checked) {
			$(".searchAllModules").text("Deselect all");
			return false; // break
		}
	});	
	
	//media file transcripts
	$(".mediaFileTranscript").hide();
	$(".toggleTranscriptView").click(function (ev) {
		ev.preventDefault();
		$(this).parent().next(".mediaFileTranscript").slideToggle(300);
	});
	
	/*for personalisation*/
/* 	Personalise = new personalise(); */
/* 	Personalise.initfrontend("#perstodofrontph","#persfavfrontph","framework/personalise/personalisehandlerforfront.cfm"); */
	
	//Staff directory initialisation
	if (typeof StaffDirectory !== "undefined") {
		StaffDirectory = new StaffDirectory(
			treeOpen, 
			treeClosed,
			"handlers/staff_dir.cfm",
			"base-install/images/staff-directory/loading.gif"
		);
	    StaffDirectory.init();
		
		//switcher for locations drop down
		$('.showLoc').click(function(){
			$('#location_div').toggle();
		});
	}
	
	//IPM initialisation
	if (typeof IPMController !== "undefined") {
		try {
			FieldManager = new fieldManager({
				handler: 'fieldManagerHandler.cfm',
				frontEndMode: true
			});
			IPM = new IPMController({
				handler: 'IPMHandler.cfm',
				parentDivId: '#IPMTemplate',
				frontEndMode: true
			});
			IPM.init();
		} catch (e) {
			alert(e.message);
		}
	}
	
	//Targets the HTML5 Placeholder element in form inputs and provides a fallback for crippled browsers
	//Taken from http://www.cssnewbie.com/cross-browser-support-for-html5-placeholder-text-in-forms/
	if(!$.support.placeholder) { 
		var active = document.activeElement;
		$(':text').focus(function () {
			if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
				$(this).val('').removeClass('hasPlaceholder');
			}
		}).blur(function () {
			if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
				/*commented out because its breaking at the moment.
				$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');*/
			}
		});
		$(':text').blur();
		$(active).focus();
		$('form').submit(function () {
			$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
		});
	}
});	

function quicklinkgo(){
	var btn = document.getElementById('quicklinks_go');
	if(!btn)
		return false;
	btn.style.display='none';//hide the button

	btn = document.getElementById('quicklinks_select');
	if(btn)btn.onchange=function(){this.parentNode.submit();}
}
