$(document).ready(function(){
	
	$('ul.leftnav > ul').hover(function() {
		$(this).next('ul').css({
			top: $(this).offset().top - 128
		}).fadeIn(500);
	}, function() {
		$(this).fadeOut(500);
	});
	
	$('#top-banner-container').append($('.padding .slide'));
	

	setTimeout(function() {
		$('#top-banner-container .slide').animate({
			'top': '0%'
		}), 'fast';
	}, 3000);
	
	/* Functions to expand out blocks to fill page when there is no enough content to push those elements to the widow bounds */
	function maximiseSpace(jqueryObjectArray) {
		for(key = 0; key <= jqueryObjectArray.length - 1; key++) {
			var additionalHeight = parseInt(jqueryObjectArray[key].css('padding-top'))
									+ parseInt(jqueryObjectArray[key].css('padding-bottom'))
									+ parseInt(jqueryObjectArray[key].css('border-top-width'))
									+ parseInt(jqueryObjectArray[key].css('border-bottom-width'));
			var realHeight = jqueryObjectArray[key].height() + additionalHeight;
			var positionBottom = jqueryObjectArray[key].position().top + realHeight;
			if(positionBottom < $(window).height()) {
				var remainingSpace = $(window).height() - positionBottom;
				jqueryObjectArray[key].height(jqueryObjectArray[key].height() + remainingSpace);
			}
		}
	}
	
	var screenFillingBlocks = new Array();
	screenFillingBlocks.push($('.page_content_stage'));
	screenFillingBlocks.push($('.page_content_stage > .film'));
	
	maximiseSpace(screenFillingBlocks);
	$(window).resize(function() {
		maximiseSpace(screenFillingBlocks);
	});
	
	$(window).load(function() {
		maximiseSpace(screenFillingBlocks);
	});
});



function upload_template_img(template_id) {
	xferWindow = window.open('/admin/includes/php/upload_template_img.php?template_id='+template_id+'','popWin','toolbar=0,status=0,height=300,width=360');
	xferWindow.opener = self;
}

function upload_ot_template_img(template_id) {
	xferWindow = window.open('/admin/includes/php/upload_ot_template_img.php?template_id='+template_id+'','popWin','toolbar=0,status=0,height=300,width=360');
	xferWindow.opener = self;
}

function open_window(psrc, popW, popH) {
	var left = parseInt((screen.availWidth/2) - (popW/2));
  var top = parseInt((screen.availHeight/2) - (popH/2));
	xferWindow = window.open(''+psrc+'','popWin','scrollbars=yes,toolbar=0,status=0,height='+popH+',width='+popW+',top='+top+',left='+left+',screenX='+left +',screenY='+ top+'');
	xferWindow.opener = self;
}

function show_hide_div(div_id) {
	if(document.getElementById(div_id).style.display == 'none') {
		document.getElementById(div_id).style.display = 'block';
	} else {
		document.getElementById(div_id).style.display = 'none';
	}
}

function confirmDelete(goto) {
	if(confirm("Delete Item?")) {	
		if(goto != "") {
			location.href = goto;
		} else {	
			return true;
		}
	} else {
		return false;
	}
}

function confirmDeleteAjax(vars, goto, divid) {
	if(confirm("Delete Item?")) {	
		if(goto != "") {
			 send_request(''+vars+'',''+goto+'?'+vars+'',''+divid+'');
		} else {	
			return true;
		}
	} else {
		return false;
	}
}

function doAjax(vars, goto, divid) {
	send_request(''+vars+'',''+goto+'?'+vars+'',''+divid+'');
}

function serialize( mixed_value ) {
    var _getType = function( inp ) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';
    
    switch (type) {
        case "function": 
            val = ""; 
            break;
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + mixed_value.length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") { 
                    continue; 
                }
                
                okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                vals += serialize(okey) +
                        serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") {
      val += ";";
  }
    return val;
}

function change_class(divid, classname) {
	document.getElementById(divid).className = classname;
}

function positionSort(param, table, primfield, find) {
	var i = 0;
	$(param).find(''+find+'').each(function() {
		i++;
		val = this.value;
		doAjax('t='+table+'&id='+val+'&pos='+i+'&primf='+primfield,'/admin/includes/ajax/pos_change.php','hiddiv');
	});
}
