var animating;
var current = 0;
function nextSlide(i){
	$('.slideshow img').eq(current).css('z-index',1);
	$('.slideshow img').eq(current).fadeOut(1500);
	$('.slideshow img').eq(i).css({'z-index':'10'});
	$('.slideshow img').eq(i).fadeIn(1000);
	current = i;
	next = i+1;
	if (next>=$('.slideshow img').length) next=0;
	setTimeout("nextSlide("+next+");", 5500);
}

function pageLoaded(){
	var cssdisabled = false; // must be proven otherwise
	var d=document;
	var testcss = d.createElement('div');
	testcss.style.position = 'absolute';
	d.getElementsByTagName('body')[0].appendChild(testcss);
	if (testcss.currentStyle) var currstyle = testcss.currentStyle['position'];
	else if (window.getComputedStyle) var currstyle = d.defaultView.getComputedStyle(testcss, null).getPropertyValue('position');
	var cssdisabled = (currstyle == 'static') ? true : false;
	d.getElementsByTagName('body')[0].removeChild(testcss);
	if (cssdisabled==true) return;


	
	/* ************************* */
	/* Homepage slideshows		*/
	/* ************************* */
	
	$('#home-page #content').css('height','457px');
	$('.slideshow img').each(function(){
		$(this).css({'position':'absolute', 'cursor':'pointer'});
	});


	$('.slideshow img:eq(0)').click(function(){
		document.location="http://www.edenshopping.co.uk/whats-on/1533/christmas-opening-times/";
	});

	$('.slideshow img:eq(1)').click(function(){
		document.location="http://www.edenshopping.co.uk/whats-on/1524/christmas-grotto/";
	});

	$('.slideshow img:eq(2)').click(function(){
		document.location="http://www.edenshopping.co.uk/whats-on/1533/christmas-opening-times/";
	});

	$('.slideshow img:eq(3)').click(function(){
		document.location="http://www.edenshopping.co.uk/whats-on/1533/christmas-opening-times/";
	});

	$('.slideshow img:gt(0)').css('display','none');
	setTimeout("nextSlide(1);", 3500);

	/* ************************* */
	/* Feature scroll -all pages */
	/* ************************* */
	
	
	
	for (var i=0;i<6;i++){
		kid = $('#fs-container ul li').eq(i);
		kid.clone().insertAfter($('#fs-container ul li:last'));
	}

	$('#fs-back').click(function(){
		if (animating==true) return;
		var p = $('#fs-container div ul').css('marginLeft');
		var num_thumbs = $('#fs-container div ul').children().length;

		max = num_thumbs-6;
		var l = max * 299;

		//var l = ($('#fs-container div ul').children().length-3) * 299;
		p = new Number(p.substr(0, p.length-2));
		if (p>-(3 * 299)){
			p -= l;
			$('#fs-container div ul').css('marginLeft', p.toString()+"px");
		}
		p+= 897;
		obj = {marginLeft: p.toString()+"px"};
		animate('fs-container', obj);
	});

	$('#fs-next').click(function(){
		if (animating==true) return;
		var p = $('#fs-container div ul').css('marginLeft');
		var num_thumbs = $('#fs-container div ul').children().length;
		max = num_thumbs-6;
		var l = max * 299;
		p = new Number(p.substr(0, p.length-2));
		if (p<=-l){
			p += l;
			$('#fs-container div ul').css('marginLeft', p.toString()+"px");
		}
		p-= 897;
		obj = {marginLeft: p.toString()+"px"};

		animate('fs-container', obj);
	});
	/* ************************* */
	/*		Fashion-Fix			 */
	/* ************************* */

	for (var i=0;i<8;i++){
		kid = $('#fashion-column2 #thumbs_column ul li').eq(i);
		kid.clone().insertAfter($('#fashion-column2 #thumbs_column ul li:last'));
	}

	$('#fashion-column2 #thumbs_column ul li').click(function(){
		var html=$(this).find('div').html();
		$('#dynamic-content').html(html);
		var im=$(this).find('img').attr('rel');
		$('#image_column img').attr('src', im);
	});
	$('#scroll-up').click(function(){
		if (animating==true) return;
		var p = $('#scroll-container ul').css('marginTop');
		var num_thumbs = $('#scroll-container ul').children().length;
		max = num_thumbs-8;
		var l = max * 88;
		p = new Number(p.substr(0, p.length-2));
		if (p>-(4 *88)){
			p -= l;
			$('#scroll-container ul').css('marginTop', p.toString()+"px");
		}
		p+= 352;
		obj = {marginTop: p.toString()+"px"};
		animate('scroll-container', obj);
	});

	$('#scroll-down').click(function(){
		if (animating==true) return;
		var p = $('#scroll-container ul').css('marginTop');
		var num_thumbs = $('#scroll-container ul').children().length;
		max = num_thumbs-8;
		var l = max * 88;
		p = new Number(p.substr(0, p.length-2));
		if (p<=-l){
			p += l;
			$('#scroll-container ul').css('marginTop', p.toString()+"px");
		}
		p-= 352;
		obj = {marginTop: p.toString()+"px"};
		animate('scroll-container', obj);
	});
	
	/* ************************* */
	/*		3 columns			 */
	/* ************************* */

	col1 = $('.3column:first')
	col2 = $('.3column:eq(1)')
	col3 = $('.3column:eq(2)')
	
	if (col1.children().length>0){	
		col1.children().last().appendTo(col2);
		h1 = col1.height();
		h2 = col2.height();

		while (h1/h2>0.5){
			col1.children().last().insertBefore(col2.children().first());
			h1 = col1.height();
			h2 = col2.height();
		}
		col2.children().first().appendTo(col1);
	
		col2.children().last().appendTo(col3);
		h2 = col2.height();
		h3 = col3.height();

		while (h2>h3){
			col2.children().last().insertBefore(col3.children().first());
			h2 = col2.height();
			h3 = col3.height();
		}
		col3.children().first().appendTo(col2);
	}
	
	/* ************************* */
	/*		2 columns			 */
	/* ************************* */

	col1 = $('.2column:first')
	col2 = $('.2column:eq(1)')

	if (col1.children().length>0){	
		numcolbreaks=col1.find('hr').length;
		arr=col1.find('hr').addClass('column-break');
		kids=col1.children();
		j=kids.length;
		columnnum=1;
		var column;
		for(i=0;i<j;i++){
			if (columnnum<=2){
				if (column){j--;i--;col1.children().eq(i).appendTo(column);}
				if (col1.children().eq(i).hasClass('column-break')){column=col2;columnnum++;i++;j++;}
			}
		}
		col1.children().detach('.column-break');

		if (numcolbreaks==0){
			col1.children().last().appendTo(col2);
			h1 = col1.height();
			h2 = col2.height();

			while (h1>h2){
				col1.children().last().insertBefore(col2.children().first());
				h1 = col1.height();
				h2 = col2.height();
			}
			col2.children().first().appendTo(col1);
		}
	}

	/* ************************* */
	/*	Lightbox	 */
	/* ************************* */
	
	lbdef = ($.fn.lightBox) ? true : false;
	if (lbdef) $('a[rel*="lightbox"]').lightBox();

}

function animate(comp,obj){
	animating = true;

	$('#' + comp + ' ul').animate(
		obj,
		500, 
		"swing",
		function(){animating=false;}
	);

}


