function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var rooturl = 'http://www.artsphotography.co.uk/';
if(Browser.Engine.trident4){
	if (document.all && document.styleSheets && document.styleSheets[0] && document.styleSheets[0].addRule){
	    new Asset.javascript('http://www.artsphotography.co.uk/templates/default/scripts/pngfix.js');
        var htcurl = 'http://www.artsphotography.co.uk/templates/default/styles/';
		document.styleSheets[0].addRule('input', 'behavior: url('+htcurl+'/pngfix.htc)');
		document.styleSheets[0].addRule('div', 'behavior: url('+htcurl+'/pngfix.htc)');
		document.styleSheets[0].addRule('img', 'behavior: url('+htcurl+'/pngfix.htc)');
	}
}

/*
 * setupMorph(eElement, sDisplayStyle)
 *
 * Sets up the morph event of an element as to hide it when 
 * its opacity is morphed to 0 or to show it when its 
 * opacity is morphed from 0
 *
 * sDisplayStyle is optional, defaults to 'block' which is
 * usually ok.
 *
 * Can be set up with class="defaultMorph" on elements
 */
function setupMorph(eElement, sDisplayStyle, iDuration)
{
	if(!$defined(iDuration)) iDuration = 1;
	if(!$defined(sDisplayStyle)) sDisplayStyle = 'block';
	eElement = $(eElement);
	eElement.setStyle('opacity', 0);
	eElement.set('morph', {'duration': iDuration * 1000});
	eElement.get('morph').addEvents({
		'onStart': function(e){var show = true; $try(function(){show = this.to.opacity[0].value != 0;}.bind(this)); if(e.getStyle('opacity') == 0 && show) e.setStyle('display', sDisplayStyle);},
		'onComplete': function(e){if(e.getStyle('opacity') == 0) e.setStyle('display', 'none');}
	});
}

/*
 * fadeImages(eElement, iDuration)
 *
 * Fades between all the images in an element showing each
 * for iDuration seconds
 */
function fadeImages(eElement, iDuration, iFadeDelay)
{
	if(!$defined(window._fadeImages_autoRotate)) window._fadeImages_autoRotate = [];
	
	if(!$defined(iFadeDelay)) iFadeDelay = 1;
	if(!$defined(iDuration)) iDuration = 5;
	
	var eImages = ($type(eElement) == 'string' ? $(eElement) : eElement);

	if(eImages.getStyle('position') != 'relative' && eImages.getStyle('position') != 'absolute' && eImages.getStyle('position') != 'fixed')
		eImages.style.position = 'relative';
	
	eImages = eImages.getElements('img');

	if(eImages.length == 0) return;

	$each(eImages, function(img){img.setStyles({'position': 'absolute', 'top': 0, 'left': 0}); setupMorph(img, 'block', iFadeDelay);});
	eImages[0].setStyles({'opacity': 1, 'display': 'block'});
	
	var rotateFunction = function(eImages){
		if(!this.curImage) this.curImage = 0;
		this.curImage += 1;
		if(this.curImage > eImages.length - 1) this.curImage = 0;
		$each(eImages, function(img, i){img.morph({'opacity': i == this.curImage ? 1 : 0})}.bind(this));
	};

	window._fadeImages_autoRotate.include(rotateFunction.periodical(iDuration * 1000, rotateFunction, [eImages]));

    window.addEvent('beforeunload', function(){
		for(var i = window._fadeImages_autoRotate.length - 1; i >= 0; i--)
        	$clear(window._fadeImages_autoRotate[i]);
    });
}

window.addEvent('domready', function(){
	$each($$('.defaultMorph'), function(eElement){setupMorph(eElement);});
	
	$each($$('.fadeImages'), function(eElement){
		var iFadeDelay = 1;
		var iDuration = 5;
		
		if(eElement.className.length > 11){
			var tDuration, tFadeDelay;
			tDuration = eElement.className.substring(eElement.className.indexOf('fadeImages')+11);		
			tDuration = tDuration.substring(0, tDuration.indexOf(' ') > -1 ? tDuration.indexOf(' ') : tDuration.length);
			if(tDuration - 0 == parseInt(tDuration)) iDuration = tDuration;
			
			tFadeDelay = eElement.className.substring(eElement.className.indexOf('fadeImages')+11);
			tFadeDelay = tFadeDelay.substring(tDuration.toString().length+1);
			tFadeDelay = tFadeDelay.substring(0, tFadeDelay.indexOf(' ') > -1 ? tFadeDelay.indexOf(' ') : tFadeDelay.length);
			if(tFadeDelay - 0 == parseInt(tFadeDelay)) iFadeDelay = tFadeDelay;
		}

		fadeImages(eElement, iDuration, iFadeDelay);
		
		$$('.homePod').each(function(homePod){
			homePod.getElement('.podTitle').set('morph', {'duration': 250});
			homePod.getElement('.podSubTitle').set('morph', {'duration': 250});
			homePod.getElement('.podArrow').set('morph', {'duration': 250});						 
			homePod.addEvents({
				'mouseenter': function(e){
					var homePod = $(e.target);
					if(!homePod.hasClass('homePod')) homePod = homePod.getParent('.homePod');
					
					homePod.getElement('.podTitle').morph({'left': 20, 'bottom': 25});
					homePod.getElement('.podSubTitle').morph({'left': 8, 'bottom': 5});
					homePod.getElement('.podArrow').morph({'right': 13, 'bottom': 30});
				},
				'mouseleave': function(e){
					var homePod = $(e.target);
					if(!homePod.hasClass('homePod')) homePod = homePod.getParent('.homePod');
					
					homePod.getElement('.podTitle').morph({'left': 10, 'bottom': 5});
					homePod.getElement('.podSubTitle').morph({'left': 5, 'bottom': -20});
					homePod.getElement('.podArrow').morph({'right': 15, 'bottom': 10});
				}
			});							 
		});
		
	});
});


//=WW=======================================================
// CreateElement(data)
// Creates a DOM element by passing an object with all 
// the information you want for the element in it.
// To avoid memory leaks in IE, pass one field as parent with a
// string identifying the ID of the parent DOM element to add to
//
// e.g. to create a tr with a class of 'header'
//		tr = CreateElement({tagtype: 'tr', className: 'header'});
//
// or to create a div with text inside
//		div = CreateElement({tagtype: 'div', text: 'Some text'});
//
// or to create a div containing two images
//		div = CreateElement(tagtype: 'div', children: 
//			[{tagtype: 'img', src: 'image1.jpg'}, {tagtype: 'img', src: 'image2.jpg'}]);
//
// Function returns the element to be used in an append
// e.g. table.appendChild(tr);
//==========================================================
function CreateElement(data)
{
	var e = document.createElement(data.tagtype);
	if(data.tagtype == 'input') e.type = (data.type == undefined ? 'text' : data.type);
	if(typeof data.parent == 'string') data.parent = document.getElementById(data.parent);
	if(data.parent != undefined) data.parent.appendChild(e);
	for(var key in data)
		if(key == 'style') for(var s in data.style) eval('e.style.'+s+' = data.style.'+s);
		else if(key == 'text') e.appendChild(document.createTextNode(data.text));
		else if(key == 'children') for(var i=0;i<data.children.length;i++) {data.children[i].parent = e; CreateElement(data.children[i]);}
		else if(key.substr(0,2) == 'on') eval('e.'+key+' = new Function("'+eval('data.'+key).replace(/\"/g, '\\"')+'")');
		else if(key != 'parent') eval('e.'+key+' = data.'+key);
	if(data.parent == undefined) return e;
}


arrSort = new Array();
strSortBy = 'competition_entrant_imageadded';
results = 8;
page = 1;
voted = false;

function DoSort(strSort)
{
	if(arrSort[strSort] == undefined)
		arrSort[strSort] = 0;
	else
		arrSort[strSort] = (arrSort[strSort] == 1 ? 0 : 1);
		
	strSortBy = strSort;
	
	GetCards('');
}

function GetEntrants(submittype)
{
	var strPost = 'act=entrants&results[0]='+results+'&results[1]='+results+'&page[0]='+page+'&page[1]='+page;
	
	if(voted == true)
		strPost += '&voted=1';
	
	switch(submittype) {
		case 'limit':
			strPost += '&set[0]=Set';
			break;
			
		case 'previous':
			strPost += '&previous[0]=1';
			break;
			
		case 'next':
			strPost += '&next[0]=1';
			break;
			
		case 'last':
			strPost += '&last[0]=1';
			break;
			
		case 'first':
			strPost += '&first[0]=1';
			break;
			
		case 'page':
			strPost += '&jump[0]=Go';
			break;
	}
	
	$('loading').style.display = '';
	
	var children = $$('#votePods .votePod');
	for(i = children.length - 1; i >= 0; i--)
		if($(children[i]).id != 'loading')
			$('votePods').removeChild(children[i]);
	new Request(
		{
			url: 'promo_hotty_vote.php?sortby='+strSortBy+'&direct='+arrSort[strSortBy],
			method: 'post',
			data: strPost,
			onComplete: function(t) {
										t = t.split('^');
										$('page').value = t[0];
										$('results').value = t[1];
										$('total').value = t[3];
										$('numpages').value = t[4];
										if(JSON.decode(t[5]) && JSON.decode(t[5]).children) {
												var children = JSON.decode(t[5]).children;
												for(i = 0; i < children.length; i++)
													CreateElement(children[i]);
										} else {
											var obj = new Object;
											var style = new Object;
											style.display = 'block';
											style.textAlign = 'center';
											style.width = '100%';
											style.position = 'relative';
											style.top = '125px';
											obj.tagtype = 'div';
											obj.parent = 'votePods';
											obj.style = style;
											obj.text = 'There are currently no entries to display!';
											$('browsePods').style.display = 'none';
											$('votePods').style.height = '250px';
											CreateElement(obj);
										}
											
										$('loading').style.display = 'none';
									}
		}
	).send();
	
	return false;
}

function fadeIn(obj)
{
	new Fx.Tween($(obj), {property: 'opacity'}).set(0);
	$(obj).style.display = '';
	var myFx = new Fx.Tween($(obj), {property: 'opacity'}).start(0, 1);
	$(obj).addEvent('mouseleave', function(e) {fadeOut(this)});
}

function fadeOut(obj)
{
	new Fx.Tween($(obj), {property: 'opacity'}).start(1, 0).addEvent('onComplete', function(e){obj.parentNode.removeChild(obj)});
}

function EntrantBrowser()
{
	arrSort['competition_entrant_imageadded'] = 0;
	
	var Cookie = new Hash.Cookie('hottest');
	
	if(Cookie.get('2008voted') == '1')
		voted = true;
	
	GetEntrants('limit');
}

function doVote(id, phase)
{
	
	var Cookie = new Hash.Cookie('hottest');
	
	switch(phase) {
		case 0:
			if(Cookie.get('2008voted') == '1') {
				alert('Sorry! You are not allowed to vote more than once!');
				break;
			}
			$('competition_vote_couple').value = id;
			new Fx.Tween($('voteWin'), {property: 'opacity'}).set(0);
			$('voteWin').style.display = '';
			new Fx.Tween($('voteWin'), {property: 'opacity'}).start(0, 1).addEvent('onComplete', function(e) {doVote(id, 1)});
			break;
		case 1:
			new Fx.Tween($('voteForm'), {property: 'width'}).set(0);
			new Fx.Tween($('voteForm'), {property: 'height'}).set(0);
			$('voteForm').style.display = '';
			new Fx.Tween($('voteForm'), {property: 'width', transition: Fx.Transitions.Elastic.easeOut}).start(0, 300);
			new Fx.Tween($('voteForm'), {property: 'height', transition: Fx.Transitions.Elastic.easeOut}).start(0, 304);
			break;
		case 2:
			$('competition_vote_name').setStyle('border', '');
			$('competition_vote_email').setStyle('border', '');
			new Request({url: 'promo_hotty_vote.php', data: $('form_vote'), onComplete: function(t){
				if(t.indexOf('complete') > -1) {
					var Cookie = new Hash.Cookie('hottest');
					Cookie.set('2008voted','1', {duration: 365});
					new Fx.Tween($('voteForm'), {property: 'width', transition: Fx.Transitions.Elastic.easeIn}).start(300, 0).addEvent('onComplete', function(e){doVote(0,3)});
					new Fx.Tween($('voteForm'), {property: 'height', transition: Fx.Transitions.Elastic.easeIn}).start(283, 0);
					var links = $$('.votelinks');
					for(i=0;i<links.length;i++)
						links[i].parentNode.removeChild(links[i]);	
					voted = true;
				} else {
					t = t.split('|');
					var str = 'The following errors were reported:';
					for(i=0;i<t.length;i++) {
						t[i] = t[i].split('~');
						str += "\r\n     " + t[i][1];
						$(t[i][0]).setStyle('border', '1px solid red');
					}
					alert(str);
				}	
			}}).send()
			break;
		case 3:
			new Fx.Tween($('voteComplete'), {property: 'width'}).set(0);
			new Fx.Tween($('voteComplete'), {property: 'height'}).set(0);
			$('voteComplete').style.display = '';
			new Fx.Tween($('voteComplete'), {property: 'width', transition: Fx.Transitions.Elastic.easeOut}).start(0, 300);
			new Fx.Tween($('voteComplete'), {property: 'height', transition: Fx.Transitions.Elastic.easeOut}).start(0, 100);
			break;
		case 4:
			new Fx.Tween($('voteComplete'), {property: 'width', transition: Fx.Transitions.Elastic.easeOut}).start(300, 0);
			new Fx.Tween($('voteComplete'), {property: 'height', transition: Fx.Transitions.Elastic.easeOut}).start(100, 0);
			new Fx.Tween($('voteWin'), {property: 'opacity'}).start(1, 0).addEvent('onComplete', function(e) {doVote(0, 5)});
			break;
		case 5:
			$('voteWin').style.display = 'none';
			break;
	}
}