﻿/* This file runs on every page, every time they are loaded. Do not remove it, it's a core file. */
/* Init - runs when document is ready. All functions which require the document to be ready should be called from here */
$(function() {
	// Labels for forms
	$(":text").labelify();
	
	initDatePicker();
	createTabs();
	testIfPageContainsColumns();
	addClassToBody("#content");
});

function initDatePicker() {
	// Datepickers for date input fields
	$('input.date').datepicker({ dateFormat: 'dd/mm/yy' });
}
 
// Open an AJAX dialog
function openDialog(url,title,width,height) {
	// Width and height
	var w = 500;
	var h= 300;
	if(width) {
		w = width;	 
	}
	if(height) {
		h = height;	 
	}
	 // Reset the dialog
    $("#dialog").html('').dialog('destroy'); 
    // Set up dialog
    $("#dialog").dialog({
		width: w,
		height: h,
		modal: true,
		title: title,
		cache: false
    }); 
    // Fetch content
    $.ajax({
		type: "GET",
		url: url,
		data: "ajax=1",
		success: function(data){
			$("#dialog").html(data);
		}
    });
}

// Replace alrt boxes with a much nicer alternative
function ajaxAlert(msg) {
	if($('#alert').length == 0) {
		$("body").append('<div id="alert"></div>');	
	}
    $('#alert').dialog('destroy').html(msg).dialog({
		dialogClass: 'alert',
		width: 200,
		minHeight: 80,
		modal: true,
		cache: false,
		buttons: { "Ok": function() { $(this).dialog("close"); } }
    }); 
}

/* create tabs function - to allow for tabs to be called by other means */
function createTabs(){
	$(".tabs").tabs();
}

// columns script - checks number of div.columns inside of div.row and adds specific class to div.inner.
function testIfPageContainsColumns() {
	// test is .row is in the DOM
	if($(".row").length>=1) {
		// run getNumberOfColumns function
		getNumberOfColumns();
	}
	else {
		// do nothing
	}
	function getNumberOfColumns() {
		var rowStore = $(".row");
		for(var i = 0; i < rowStore.length; i++) {
			// var ready to catch number of columns
			var numberOfChildren = new Number();
			// get the number of .columns and pass is to var
			numberOfChildren = $(rowStore[i]).find(".inner .column").length;
			// if the number of columns is two or less than; do nothing
			if(numberOfChildren<=2) {
				// do nothing
			}
			// if the number of columns is more than two
			else {
				// add the a specific class according to the amount of children
				$(rowStore[i]).find(".inner:first-child").addClass("numberOfColumns" + numberOfChildren);
			}
		}
	}
}

// removes class from div#content and adds it to body tag
function addClassToBody(targetedDiv) {
	if($("#content").length == 1) {
		var contentDivElement = $(targetedDiv);
		var className = new String();
		var templateClassNames = new Array();
		
		className = contentDivElement.attr("class");
		
		$("body").addClass(className);
		contentDivElement.removeClass(className);
		
		if(contentDivElement.attr("class", "")) {
			contentDivElement.removeAttr("class");
		}
		else {
			// do nothing
		}
	}
	else {
	// do nothing
	}
};