$(document).ready(function(){
	function fillScreen() {
		$('.remainder').css({
			'height': ($(window).height() - $('.remainder').position().top - 2) + 'px'
		});
		
		/* Popout navigation functions */
		var availableHeight = $(window).height() - parseInt($('.pop-out').css('padding-top'), 10);
		var fitsInHeight = Math.floor(availableHeight / solutionBlockHeight);
		var columnsNeeded = Math.ceil($('.pop-out li').length / fitsInHeight);
		var availableWidth = (columnsNeeded * solutionBlockWidth);
		var combinedWidth =  availableWidth + parseInt($('.pop-out').css('padding-left'), 10);
		popoutXOrigin = 0 - combinedWidth;
		
		$('.pop-out-container').css({
			'width': combinedWidth + 'px',
			'display': 'hidden'
		});
		$('.pop-out').css({
			'height': availableHeight + 'px',
			'width': availableWidth + 'px',
			'left': popoutXOrigin + 'px'
		});
		
	}
	
	$('#primary-nav-container > ul > li').prepend('<div>&nbsp;</div>').children('div').addClass('led png');
	
	$(window).resize(function() {
		fillScreen();
	});
	
	/* Navigation mouseover function */
	$('#primary-nav-container > ul > li').hover(function() {
		$(this).children('.led').css({
			'display': 'block'
		});
	}, function() {
		$(this).children('.led').css({
			'display': 'none'
		});
	});
	
	/* Tab function */
	$('.tabs > div:not(:first)').hide();
	$('.tabs li a').click(function(eventData) {
		$('.tabs > div').hide();
		$('.tabs > ul .current').removeClass('current');
		$(this).addClass('current');
		
		var clicked = $(this).attr('href');
		$('.tabs > div#' + clicked).fadeIn('fast');
		eventData.preventDefault();
	}).eq(0).addClass('current');
	
	/* Expandable content function */
	$('div.expandable').addClass('closed').after('<p class="expandable"><a href="#">Read More</a></p>');
	$('p.expandable').toggle(function() {
		$(this).prev().removeClass('closed');
		$(this).children().html('Minimise');
	}, function() {
		$(this).prev().addClass('closed');
		$(this).children().html('Read More');
	});
	
	/* IE 6 functions */
	$(window).scroll(function() {
		$('#primary-nav-container').css('top', $(this).scrollTop() + "px");
		if($.browser.msie && Math.round($.browser.version, 0, 1) == 6) {
			$('.background-shadow').css('top', $(this).scrollTop() + "px");
		}
	});
	
	if($.browser.msie && Math.round($.browser.version, 0, 1) == 6) {
		$.getScript('/includes/js/DD_belatedPNG_0.0.8a-min.js', function() {
			DD_belatedPNG.fix('.png');
		});
		$('.background-shadow').css('position', 'absolute');
		$('#primary-nav-container > *').css('float', 'left');
	}
	
	/* Popout navigation functions */
	
	$('.pop-out').prependTo($('.pop-out-container')).addClass('js');
	/*define variables for the navigation pop-out functions */
	var popoutXOrigin = 0;
	var solutionBlockHeight = parseInt($('.pop-out li').css('height'), 10) + parseInt($('.pop-out li').css('margin-bottom'), 10);
	var solutionBlockWidth = parseInt($('.pop-out li').css('width'), 10) + parseInt($('.pop-out li').css('margin-right'), 10);
	$('#primary-nav-container > ul > li').eq(0).hover(function() {
		$('.pop-out-container').css({'display': 'block'});
		$('.pop-out').stop().animate({
			'left': '0px'
		}, 'fast');
	}, function() {
		$('.pop-out').stop().delay(100).animate({
			'left': popoutXOrigin + 'px'
		}, 'fast', function() {
			$('.pop-out-container').css({'display': 'none'});
		});
	});
	
	$('.pop-out').hover(function() {
		$('.pop-out-container').css({'display': 'block'});
		$('.pop-out').stop(true).animate({
			'left': '0px'
		}, 'fast');
	}, function() {
		$('.pop-out').stop().delay(100).animate({
			'left': popoutXOrigin + 'px'
		}, 'fast', function() {
			$('.pop-out-container').css({'display': 'none'});
		});
	});
	
	/* Run when everyting is loaded */
	fillScreen();
});

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');
	});
}
