//--- request parameters
var params = new Array();
var params_asarray = window.location.href.match(/([\?&][^=]+=[^&#]*)/gi);
if (params_asarray) {
	for (var i=0; i<params_asarray.length; i++) {
		var insidehits = params_asarray[i].match(/[\?&]([^=]+)=([^&#]*)/i);
		if (insidehits) {
			params[insidehits[1]] = insidehits[2];
		}
	}
}

var addthis_config = { 
	services_exclude: 'print' 
} 


//--- are we in a frame (e.g. in a popup)
var isiniframe = (window.location.href != parent.location.href) ? true : false;

//--- popup but not in a popup? close the popup and reload in bottom
if (isiniframe && params['popup']) {
	var newurl= window.location.href;
	newurl = newurl.replace(/\?popup=1/i, '?');
	newurl = newurl.replace(/&popup=1/, '');
	newurl = newurl.replace(/\?$/, '');
	window.location = newurl;
}

//--- document ready!
$(document).ready(function() {
	//--- popup uid discovery
	if (isiniframe) {
		myframe_uid = window.top.lastpopupopened_uid;
	}

	//--- click anywhere closes the admin controls
	$(document).click(function () { 
		hideAllAdminControls();
	} );

	//--- colorbox related stuff
	$("a.picturepreview").colorbox({
		current: "Bild {current} von {total}",
		previous: "voriges",
		next: "nächstes",
		close: "schließen"
	});

	//--- overlay animation over some buttons
	$("a.nextprevious").mouseover(function () { 
		$(this).find('img').animate({opacity: 0.5}, 'fast');
	});
	$("a.nextprevious").mouseout(function () { 
		$(this).find('img').animate({opacity: 1}, 'fast');
	});

	$("a.transhover").mouseover(function () { $(this).animate({opacity: 0.5}, 'fast'); }).mouseout(function () { $(this).animate({opacity: 1}, 'fast'); });


	$("img.thumbnailsized").mouseover(function () { $(this).animate({opacity: 0.5}, 'fast'); });
	$("img.thumbnailsized").mouseout(function () { $(this).animate({opacity: 1}, 'fast'); });

	//--- sortable lists triggers
	$("ul.slideshow[id*='cmssl']").sortable({
		update: function(event, ui) { 
			$.get("./?action=serializeupdate&" + ui.item.parent().sortable('serialize'), function(data) {
				if (data) {
					alert(data);
				}
			});
		}
	});


	//--- navigation animation and magick :)
	$('#mainnavi a.toplevel').click(function () {
		var div = $('#sub_' + this.id);
		if (!div.length) return true; //--- no id - no subs!
		if (div.attr('animating') == 1) return false; //--- animating - dont animate again

		div.attr('animating', 1);
		var maxheight = div.find('div').height();

		if (div.height() > 1) {
			$(this).find('img').attr('src', '/images/layout/naviarrow.png');
			div.animate({ height: 1 }, 300, false, function () { div.attr('animating', 0); });
		} else {
			$(this).find('img').attr('src', '/images/layout/naviarrow_sel.png');
			div.animate({ height: maxheight }, 300, false, function () { div.attr('animating', 0); });
		}
		this.blur();
		return false;
	});


	//--- login form needs opening?
	if (params['dologin']) {
		openpopup('loginform', './?form=login', 500, 120, 'Login', '', '');
	}


	//--- font size changing
	$('.size1').click(function () {
		$('.content').find('p').css({'font-size': '11px'});
		$('.content').find('blockquote').css({'font-size': '11px'});
		$('.content').find('h1').css({'font-size': '15px'});
		$('.content').find('h2').css({'font-size': '15px'});
		$('.content').find('h3').css({'font-size': '14px'});
		$('.content').find('h4').css({'font-size': '11px'});
		$('.startpage1').add('.startpage2').add('.startpage3').css({'height': 214});
		return false;
	});
	$('.size2').click(function () {
		$('.content').find('p').css({'font-size': '12px'});
		$('.content').find('blockquote').css({'font-size': '12px'});
		$('.content').find('h1').css({'font-size': '180px'});
		$('.content').find('h2').css({'font-size': '18px'});
		$('.content').find('h3').css({'font-size': '15px'});
		$('.content').find('h4').css({'font-size': '12px'});
		$('.startpage1').add('.startpage2').add('.startpage3').css({'height': 244});
		return false;
	});
	$('.size3').click(function () {
		$('.content').find('p').css({'font-size': '14px'});
		$('.content').find('blockquote').css({'font-size': '14px'});
		$('.content').find('h1').css({'font-size': '23px'});
		$('.content').find('h2').css({'font-size': '23px'});
		$('.content').find('h3').css({'font-size': '19px'});
		$('.content').find('h4').css({'font-size': '14px'});
		$('.startpage1').add('.startpage2').add('.startpage3').css({'height': 324});
		return false;
	});

	//--- weather loader
	$('#weatherloader').each(function () {
		var me = $(this);
		$.ajax({
			url				: './?load=weather',
			type			: 'GET',
			data			: { },
			success			: function(content) { me.html(content); },
			error			: function() { me.html(''); me.hide(); }
		});
	});
	
	//--- news loader
	$('#newsloader').each(function () {
		var me = $(this);
		$.ajax({
			url				: './?load=rssnews',
			type			: 'GET',
			data			: { },
			success			: function(content) { me.html(content); },
			error			: function() { me.html(''); me.hide(); }
		});
	});

	$('.startpage_left').add('.startpage_right').mousedown(function() { 
		$(this).attr('pressed', 1);
		if (!$(this).attr('originalposition_top')) { $(this).attr('originalposition_top', $(this).position().top); }
		if (!$(this).attr('originalposition_left')) { $(this).attr('originalposition_left', $(this).position().left); }

		$(this).animate({'top' : $(this).position().top + 4, 'left' : $(this).position().left + 4}, 100); 
	}).mouseup(function() { 
		$(this).attr('pressed', 0);
		$(this).animate({'top' : $(this).attr('originalposition_top'), 'left' : $(this).attr('originalposition_left')}, 300);
	});

	$('.startpage_left').mousedown(function() {
		$('#header_startpage > img:visible').each(function() { 
			startpagehdear_swaptonext($(this), 'left');
		});
	});
	$('.startpage_right').mousedown(function() {
		$('#header_startpage > img:visible').each(function() { 
			startpagehdear_swaptonext($(this), 'right');
		});
	});
	

	$('#showroomcontainer > div.inner').css({'position': 'absolute', 'left': 0, 'top': 0});
	setTimeout('animateshowroom()', 1500);

	$('.shlideshowthumb').click(function() {
		$('.showroom_container > img.big').attr({'src': $(this).attr('rel')});
		return false;
	});

	$('#header_startpage > img').each(function () {
		$(this).attr('rel', $(this).attr('alt')).attr('alt', $(this).attr('title'));
	});
/*
	//--- overlay animation over some buttons
	$("#audiocaptcha").each(function () { 
		flowplayer(this.id, "/js/flowplayer/flowplayer-3.2.5.swf", {
		    clip : {
		        autoPlay: false,
		        autoBuffering: false
		    },
			plugins : {
				controls : {
					url: '/js/flowplayer/flowplayer.controls-3.2.3.swf',
					all: false,
					play: true
				}
				audio : {
					url: '/js/flowplayer/flowplayer.audio-3.2.1.swf'
				}
			}
		});
	});
*/

	//--- calendars if we have them
	/*
	$("input.general_input[name*=_year]").datepicker({
		showOn: 'button',
		buttonImage: '/images/layout/calendar.png',
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		onSelect: function(dateText, inst) {
			alert(dateText);
			return false;
		}
	});
	*/

	//--- newsletterseting needed?
	$("#newsletterprogress").each(function () {
		init_newslettersending(this.className);
	});

	$("input.uidatepicker").datepicker({
		dateFormat: 'yy-mm-dd'
	});

	$(".focusmefirst").focus();

});

var startpageheader_timeout = false;
function startpagehdear_swaptonext(source, direction) {
	clearTimeout(startpageheader_timeout);
	var target = false;
	if (direction == 'left') {
		var target = source.prev(); if (!target.length) { target = $('#header_startpage > img').last(); }
	} else {
		var target = source.next(); if (!target.length) { target = $('#header_startpage > img').first(); }
	}
	if (!target) { return; }


	if (target.attr('src'))	{
		source.fadeOut('slow'); target.fadeIn('slow', function() {
			startpageheader_timeout = setTimeout(function() { if ($('.startpage_' + direction).attr('pressed') == 1) { startpagehdear_swaptonext(target, direction); } }, 300);
		});
	} else {
		$.loadImages([target.attr('rel')], function() {
			target.attr('src', target.attr('rel'));
			source.fadeOut('slow'); target.fadeIn('slow', function() {
				startpageheader_timeout = setTimeout(function() { if ($('.startpage_' + direction).attr('pressed') == 1) { startpagehdear_swaptonext(target, direction); } }, 300);
			});
		});
	}
}

function animateshowroom() {
	$('#showroomcontainer > div.inner').each( function () {
		if ($(this).position().left <= - ($(this).width() / 2) ) {
			$(this).css({'left': 0});
		}
		$(this).animate({'left': $(this).position().left - 130}, 'slow', null, function() {
			setTimeout('animateshowroom()', 1500);
		});
	});
}

var myframe_uid = false;
var lastpopupopened_uid = false;
var disableclosingpopup = false;

var nextpopupid = 1;
var popupsopener = [];
var popupimagespreloaded = false;
//--- open interface popup function
function openpopup(uid, url, width, height, title, icon, helplink, openeruid) {
	//--- already inside a popup?
	if (isiniframe) {
		window.top.openpopup(uid, url, width, height, title, icon, helplink, myframe_uid);
		return false;
	}
	if (!openeruid) openeruid = lastpopupopened_uid;
	popupsopener[uid] = openeruid;

	if ($('#popup_'+uid)) {	$('#popup_'+uid).remove(); }
	if ($('#popupbox_'+uid)) { $('#popupbox_'+uid).remove(); }
	
	//--- 1. create the overlay container div
	var mainoverlaydiv = $(document.createElement('div'));
	mainoverlaydiv.attr('id', 'popup_' + uid);
	mainoverlaydiv.css({
		'display': 'none',
		'position': 'fixed',
		'left': '0',
		'top': '0',
		'width': '100%',
		'height': '100%',
		'opacity': 0.8,
		'background-color': '#CCCCCC',
		'z-index': 9999 + nextpopupid++,
		'cursor': 'pointer'
	});
	mainoverlaydiv.click(function () { closepopup(uid); });
	$('body').append(mainoverlaydiv);


	var totalwidth = width + 2 * 14;
	var totalheight = height + 34 + 14;

	var boxleft = parseInt( (mainoverlaydiv.width() / 2) - (totalwidth / 2) );
	var boxtop = parseInt( (mainoverlaydiv.height() / 2) - (totalheight / 2) );

	//--- 2. create the box itself as a second div
	var boxcontainerdiv = $(document.createElement('div'));
	boxcontainerdiv.attr('id', 'popupbox_' + uid);
	boxcontainerdiv.css({
		'display': 'none',
		'position': 'fixed',
		'left': boxleft,
		'top': boxtop,
		'width': totalwidth,
		'height': totalheight,
		'z-index': 9999 + nextpopupid++
	});
	$('body').append(boxcontainerdiv);

	//--- borders of the box
	boxcontainerdiv.append($(document.createElement('div')).addClass('popup_tl').css({ 'position': 'absolute', 'top': 0, 'left': 0, 'width': 14, 'height': 34, 'background-image': 'url(/images/admin/popup/border_tl.png)'}));
	boxcontainerdiv.append($(document.createElement('div')).addClass('popup_t').css({ 'position': 'absolute', 'top': 0, 'left': 0 + 14, 'width': width, 'height': 34, 'background-image': 'url(/images/admin/popup/border_t.png)'}));
	boxcontainerdiv.append($(document.createElement('div')).addClass('popup_tr').css({ 'position': 'absolute', 'top': 0, 'left': 0 + 14 + width, 'width': 14, 'height': 34, 'background-image': 'url(/images/admin/popup/border_tr.png)'}));

	boxcontainerdiv.append($(document.createElement('div')).addClass('popup_l').css({ 'position': 'absolute', 'top': 0 + 34, 'left': 0, 'width': 14, 'height': height, 'background-image': 'url(/images/admin/popup/border_l.png)'}));
	boxcontainerdiv.append($(document.createElement('div')).addClass('popup_r').css({ 'position': 'absolute', 'top': 0 + 34, 'left': 0 + 14 + width, 'width': 14, 'height': height, 'background-image': 'url(/images/admin/popup/border_r.png)'}));

	boxcontainerdiv.append($(document.createElement('div')).addClass('popup_bl').css({ 'position': 'absolute', 'top': 0 + 34 + height, 'left': 0, 'width': 14, 'height': 15, 'background-image': 'url(/images/admin/popup/border_bl.png)'}));
	boxcontainerdiv.append($(document.createElement('div')).addClass('popup_b').css({ 'position': 'absolute', 'top': 0 + 34 + height, 'left': 0 + 14, 'width': width, 'height': 15, 'background-image': 'url(/images/admin/popup/border_b.png)'}));
	boxcontainerdiv.append($(document.createElement('div')).addClass('popup_br').css({ 'position': 'absolute', 'top': 0 + 34 + height, 'left': 0 + 14 + width, 'width': 14, 'height': 15, 'background-image': 'url(/images/admin/popup/border_br.png)'}));

	//--- close link
	boxcontainerdiv.append($(document.createElement('a')).addClass('popup_close').css({ 'position': 'absolute', 'display': 'block', 'top': 0 + 11, 'left': 0 + 14 + width + 3 - 16, 'width': 16, 'height': 16, 'text-decoration': 'none'}).attr('href', 'javascript: closepopup(\''+uid+'\')').append($(document.createElement('img')).css({'width': 16, 'height': 16, 'border': 0}).attr('src', '/images/admin/popup/close.png')));

	//--- icon
	icon = icon ? '/images/admin/popup/icons/'+icon : '/images/admin/popup/icons/default.png';
	boxcontainerdiv.append($(document.createElement('img')).addClass('popup_icon').css({ 'position': 'absolute', 'display': 'block', 'top': 0 + 11, 'left': 0 + 11, 'width': 16, 'height': 16, 'border': 0}).attr('src', icon));

	//--- help link
	if (helplink) {
		boxcontainerdiv.append($(document.createElement('a')).addClass('popup_icon').css({ 'position': 'absolute', 'display': 'block', 'top': 0 + 11, 'left': 0 + 14 + width + 3 - 16 - 3 - 16, 'width': 16, 'height': 16, 'text-decoration': 'none'}).attr('href', helplink).append($(document.createElement('img')).css({'width': 16, 'height': 16, 'border': 0}).attr('src', '/images/admin/popup/help.png')));
	}

	//--- title row
	boxcontainerdiv.append($(document.createElement('div')).addClass('popup_title').css({ 'position': 'absolute', 'display': 'block', 'top': 0 + 11, 'left': 0 + 11 + 3 + 16, 'width': width - 50, 'height': 16, 'overflow': 'hidden', 'text-align': 'left', 'font-size': 12, 'color': '#333333', 'font-family': 'Arial, sans-serif'}).html(title));

	//--- container
	var containerdrame = $(document.createElement('iframe'));
	containerdrame.addClass('popup_container');
	containerdrame.attr('frameborder',  0);
	containerdrame.attr('marginwidth',  0);
	containerdrame.attr('marginheight',  0);
	containerdrame.attr('scrolling',  'auto');
	containerdrame.css({ 'position': 'absolute', 'top': 0 + 34, 'left': 0 + 14, 'width': width, 'height': height, 'background-color': '#FFFFFF', 'border': 0});
	boxcontainerdiv.append(containerdrame);

	mainoverlaydiv.fadeIn(300, function() {
		lastpopupopened_uid = uid;
		if (popupimagespreloaded) {
			boxcontainerdiv.show();
			popup_loadcontents(uid, url);
		} else {
			$.loadImages([
				'/images/admin/popup/border_tl.png',
				'/images/admin/popup/border_t.png',
				'/images/admin/popup/border_tr.png',
				'/images/admin/popup/border_l.png',
				'/images/admin/popup/border_r.png',
				'/images/admin/popup/border_bl.png',
				'/images/admin/popup/border_b.png',
				'/images/admin/popup/border_br.png',
				'/images/admin/popup/close.png'
			], function() { popupimagespreloaded = true; boxcontainerdiv.show(); popup_loadcontents(uid, url); } )
		}
	});

	return false;
}

function popup_resize(uid, width, height) {
	//--- already inside a popup?
	if (isiniframe) {
		window.top.popup_resize(uid, width, height);
		return;
	}

	var mainoverlaydiv = $('#popup_' + uid);
	var maincontainer = $('#popupbox_' + uid);
	if (!maincontainer || !mainoverlaydiv) return;

	var totalwidth = width + 2 * 14;
	var totalheight = height + 34 + 14;

	var boxleft = parseInt( (mainoverlaydiv.width() / 2) - (totalwidth / 2) );
	var boxtop = parseInt( (mainoverlaydiv.height() / 2) - (totalheight / 2) );

	maincontainer.css({'top': boxtop, 'left': boxleft, 'width': totalwidth, 'height': totalheight});
	maincontainer.find('.popup_t').css({'width': width});
	maincontainer.find('.popup_tr').css({'left': 0 + 14 + width});

	maincontainer.find('.popup_l').css({'height': height});
	maincontainer.find('.popup_r').css({'left': 0 + 14 + width ,'height': height});

	maincontainer.find('.popup_bl').css({'top': 0 + 34 + height});
	maincontainer.find('.popup_b').css({'top': 0 + 34 + height, 'width': width});
	maincontainer.find('.popup_br').css({'left': 0 + 14 + width, 'top': 0 + 34 + height});

	maincontainer.find('.popup_close').css({'left': 0 + 14 + width + 3 - 16});
	maincontainer.find('.popup_help').css({'left': 0 + 14 + width + 3 - 16 - 3 - 16});

	maincontainer.find('.popup_container').css({'width': width, 'height': height});
}

function popup_reload(uid) {
	//--- already inside a popup?
	if (isiniframe) {
		window.top.popup_reload(uid);
		return;
	}

	lastpopupopened_uid = uid;

	var maincontainer = $('#popupbox_' + uid);
	if (!maincontainer.size()) return;

	var datacontainer = maincontainer.find('.popup_container');
	datacontainer[0].contentWindow.location.reload(true);
}

function popup_loadcontents(uid, newurl, newwidth, newheight, newtitle, newicon, newhelplink) {
	//--- already inside a popup?
	if (isiniframe) {
		window.top.popup_loadcontents(uid, newurl, newwidth, newheight, newtitle, newicon, newhelplink);
		return;
	}

	var maincontainer = $('#popupbox_' + uid);
	if (!maincontainer) return;

	var datacontainer = maincontainer.find('.popup_container');
	datacontainer.contents().find('html body').html('&nbsp;');

	//--- resize needed?
	if ((newwidth && datacontainer.width() != newwidth) || (newheight && datacontainer.height() != newheight)) {
		popup_resize(uid, newwidth, newheight);
	}

	//--- new title?
	if (newtitle) {
		maincontainer.find('.popup_title').html(newtitle);
	}

	//--- new icon?
	if (newicon) {
		maincontainer.find('.popup_icon').attr('src', '/images/admin/popup/icons/'+icon);
	}

	//--- put loading image
	var loadingimage = $(document.createElement('img'));
	loadingimage.attr('src', '/images/layout/loading_new.gif');
	loadingimage.css({ 'position': 'absolute', 'display': 'block', 'top': maincontainer.height() / 2 - 8, 'left': maincontainer.width() / 2 - 52, 'width': 105, 'height': 16, 'border': 0, 'opacity': 0.7});
	maincontainer.append(loadingimage);

	//--- load the iframe
	datacontainer.attr('src', newurl);

	datacontainer.load(function () {
		loadingimage.remove();
		datacontainer.show();
	});
}


function closepopup(uid, reloadme) {
	//--- closing popups disabled?
	if (window.top.disableclosingpopup) {
		return;
	}
	//--- called from inside a popup?
	if (isiniframe) {
		window.top.closepopup(uid, reloadme);
		return;
	}

	if (uid) {
		if ($('#popupbox_'+uid)) {
			$('#popupbox_'+uid).hide();
			//--- remove tinymce editor here!!
		}
		if ($('#popup_'+uid)) {
			$('#popup_'+uid).fadeOut();
		}

		if (popupsopener[uid]) {
			if (reloadme) popup_reload(popupsopener[uid]);
		} else {
			if (reloadme) window.location.reload();
		}
	} else {
		if (lastpopupopened_uid) {
			closepopup(lastpopupopened_uid, reloadme);
		}
	}
}

//--- admin control layers
function showAdminControl(controlid) {
	hideAllAdminControls(controlid);
	$('#'+controlid).show();
}

function hideAdminControl(controlid) {
	$('#'+controlid).hide();
}

function hideAllAdminControls(except) {
	$('div.admincontrol').each(function () {
		if (this.id != except) {
			$(this).hide();
		}
	});
	$('div._admincontrol').each(function () {
		if (this.id != except) {
			$(this).hide();
		}
	});
}


function slideshow_move(plsorminus) {
	var wemove = $('#slides');
	if (wemove.width() <= 290) {
		return;
	}

	var howmuch = 200;
	var remainderright = wemove.width() + wemove.position().left - 290;
	if (plsorminus == -1 && howmuch > remainderright) {
		howmuch = remainderright;
	}
	if (plsorminus == 1 && howmuch > - wemove.position().left) {
		howmuch = - wemove.position().left;
	}
	if (howmuch == 0) {
		return;
	}

	var movestring = '' + (plsorminus == -1 ? '-' : '+') + '=' + howmuch;
	wemove.animate({
		left: movestring
	}, 500, function() {});
}
function slideshw_showslide(picid) {
	$('#slides > a > img').each(function(index) {
		$(this).css({opacity: 1});
	});
	$('#picture_'+picid).css({opacity: 0.25});
	$('#previewimage').attr('src', './?showmediimage=' + picid);
}

function etoggle(whoid) {
	$('#'+whoid).toggle();
}
function formSubmit(f) { 
	var form = $('body');

	var formoverlay = $(document.createElement('div'));
	formoverlay.css({ 'position': 'absolute', 'display': 'none', 'top':0, 'left': 0, 'width': form.width(), 'height': $(document).height(), 'background-color': '#FFFFFF', 'opacity': 0.8});
	form.append(formoverlay);

	var loadingimage = $(document.createElement('img'));
	loadingimage.attr('src', '/images/layout/loading_new.gif');
	loadingimage.css({ 'position': 'absolute', 'display': 'block', 'top': $(document).height() / 2 - 8, 'left': form.width() / 2 - 52, 'width': 105, 'height': 16, 'border': 0, 'opacity': 0.7});
	formoverlay.append(loadingimage);

	formoverlay.fadeIn('fast');

	return false;
}
function popup(url, name, w, h) {
	window.open(url, name, 'width='+w+',height='+h+',menubar=no,scrollbars=no,toolbars=no');
}

function mcvrt(f) {
	var i = f.innerHTML;
	if (!i)	{
		return;
	}

	i = i.replace(/ at /i, '@');
	i = i.replace(/ dot /i, '.');

	f.innerHTML = i;
}


function init_newslettersending(newsletter) {
	window.top.disableclosingpopup = true;
	newslettersending_nextstep(newsletter);
}
function newslettersending_error(error) {
	window.top.disableclosingpopup = false;
	$('#newsletterprogress').hide();
	$('#newslettererrormessage').html(error + '<br><br>');
	$('#newslettererror').show();
}
function newslettersending_nextstep(newsletter) {
	var url = './?action=sendnewsletter&sub=send&newsletter=' + newsletter;
	$.ajax({
		url				: url,
		type			: 'GET',
		data			: { },

		success			: function(content) {
			var result = null, sent = 0, total = 0;

			result = /<error>(.+?)<\/error>/.exec(content);
			if (result && result[1]) {
				newslettersending_error(result[1]);
				return;
			}


			result = /<sent>(.+?)<\/sent>/.exec(content);
			if (result && result[1]) sent = parseInt(result[1]);
			result = /<total>(.+?)<\/total>/.exec(content);
			if (result && result[1]) total = parseInt(result[1]);

			if (sent > 0 && total > 0) {
				if (sent >= total) {
					$('#newsletterprogress').hide();
					$('#newsletterdone').show();
					window.top.disableclosingpopup = false;
				} else {
					$('#sent').html(sent);
					$('#total').html(total);
					newslettersending_nextstep(newsletter)
				}
			} else {
				newslettersending_error('Invalid reponse from server.');
			}
		},
		error			: function() { 
			newslettersending_error('Conneciton to the server was not possible.');
		}
	});
}



(function($){
    //cache needed for overagressive garbage collectors.
    var cache = [];
    //images can either be an array of paths to images or a  single image. 
    $.loadImages = function(images, callback){
    
        //convert to array if needed so rest of script works
        if (!(images instanceof Array)) {
            images = [images];
        }
        
        var imagesLength = images.length;
        var loadedCounter = 0;
        
        for (var i = imagesLength; i--;) {
			var cacheImage = document.createElement('img');
			//set the onload method before the src is called otherwise will fail to be called in IE
            cacheImage.onload = function(){
                loadedCounter++;
                if (loadedCounter >= imagesLength) {
                    if ($.isFunction(callback)) {
                        callback();
                    }
                }
            }
            cacheImage.src = images[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)

