/*
Version Date			Developer			Comment
----------------------------------------------------------------------
d1		26-Jan-2010		Ben Browning		Created
----------------------------------------------------------------------
Created to house any misc javascript code that we want run/accessible 
on every page.
*/

/* 
======================================================================
JAVASCRIPT STYLES
----------------------------------------------------------------------
Anonymous function that dynamically inserts a stylesheet link to 
js-enabled.css
Useful for initially hiding expanding content without any flicker
*/
(function () {
	var head = document.getElementsByTagName("head")[0];
	if (head) {
		var scriptStyles = document.createElement("link");
		scriptStyles.rel = "stylesheet";
		scriptStyles.type = "text/css";
		scriptStyles.href = "../css/js-enabled.css";
		head.appendChild(scriptStyles);
	}
}());


function clearFileInput( id )
{
    var elem = document.getElementById( id );
    elem.parentNode.innerHTML = elem.parentNode.innerHTML;
}

function popupGeneric(elm,options,href,windowRef) 
/*
----------------------------------------------------------------------
	d1		30-September-2010		Ben Browning		Copied over from Asset Bank bright_util.js
----------------------------------------------------------------------
Notes:
Generic popup window function. 

Arguments:
elm(required): is a reference to the elements for which the popup will be triggered onclick. 
	This means the function should be called once the page has loaded (so 
	the elements exist in the DOM). It can only contain one element, but it must be an array.
options(optional): for opening e.g. "width=x, height=y"
href(optional): specify the url (for links uses href value)
windowRef(optional): specify the reference to the window
*/
{
	var newWindow = window.open(href || elm.href, windowRef || "newWindow",options || "width=500,height=500,toolbar=no,location=no,status=no,menubar=no,resizable=yes,scrollbars=yes,titlebar=no");
	if(newWindow.blur){newWindow.focus();};
	return false;

}


function addRemoveInit() {
/*
----------------------------------------------------------------------
	d1		15-March-2011		Ben Browning		
----------------------------------------------------------------------
	Sets up a listener for any add or remove links on the page - used on carousel content.
*/		
	// Add/remove from lightbox - listen for clicks on add or remove links.
	$(document).delegate('a.add, a.remove', 'click', function(){
		var $thisLink = $(this);				// reference to current link
		
		$thisLink.html('<em>Loading...</em>');
		// Get myUrl and throw away forward action
		var myUrl = $thisLink.attr('href');		
		var cutoff = myUrl.indexOf('forward');
		myUrl = myUrl.substr(0,cutoff);
		// Replace forward action based on type of link clicked:
		if ($thisLink.hasClass('add')) {
			myUrl = myUrl + 'forward=%2Faction%2FaddAssetToFolderAjax';
		} 
		if ($thisLink.hasClass('remove')) {
			myUrl = myUrl + 'forward=%2Faction%2FremoveAssetFromFolderAjax';
		}
		// Load the modified url and replace the current link with the returned html.
		$.ajax({
			url: myUrl,
			success: function(html){
				$thisLink.replaceWith(html);
			}
		});
		return false;
	})

}

// Folder dropdown behaviour
function initFolderUI() {

	var $folderWrap = $('#folderWrap')
	
	//Prevent css hover reveal behaviour if user has js
	$('#folderWrap').removeClass('cssHover');
	

	$('#folderToggle').click(function() {
		var $this = $(this);
		if ($this.hasClass('on')) {
			$('#folderDropdown').hide();
			$this.removeClass('on');
		} else {
			$('#folderDropdown').show();
			$this.addClass('on');
		}
		return false;
	});
	
	$('#createFolder').click(function(e){
		e.preventDefault();
		$('#createFolderForm').slideToggle('fast');
		return false;
	});
	
	$('#cancelCreateFolder').click(function(){
		$('#createFolderForm').slideUp('fast');
		return false;
	});
	
	// Clicking outside the dropdown closes it:
	$(document).click(function(e) {
	   	if($(e.target).closest('#folderDropdown').length == 0) {
	        $('#folderDropdown').hide();
			$('#folderToggle').removeClass('on');
	    }
	});
	
}

// Search box behaviour
function initSearchBox () {
	
	var placeholder = 'Search mpdclick...';
	
	$('#searchfield').click(function () {
		var $this = $(this);
		if ($this.val() == placeholder) {
			$this.val('');
		}	
	});
	$('#searchfield').blur(function () {
		var $this = $(this);
		if ($this.val() == '') {
			$this.val(placeholder);
		}
	})
	$('#search').submit(function () {
		var $searchField = $('#searchfield');
		if ($searchField.val() == placeholder) {
			$searchField.val('');
		} 
	})
}

$(function(){
	// The following script is called on DOM load on every page - use sparingly.
	
	initFolderUI();
	
	initSearchBox();
	

	
});


