/* this code is copyrighted. created by oak-ny.com */

var fadeSpeed = 2000;
var rotateSpeed = 8000;

function showcase() {
	$('#showcase').children().css({opacity: 0.0});
	$('#showcase').children(':first').addClass('show').animate({opacity: 1.0}, fadeSpeed);
	setInterval('rotate()',rotateSpeed);
}

function rotate() {	
	var current = ($('#showcase .show')?  $('#showcase .show') : $('#showcase').children(':first'));
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#showcase').children(':first') :current.next()) : $('#showcase').children(':first'));	
	
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, fadeSpeed);

	current.animate({opacity: 0.0}, fadeSpeed).removeClass('show');
};

$(document).ready(function() {
	if ($('body.home #showcase').length > 0) showcase();
	
	$('#gallery li').hide().fadeIn('slow');
	$('#preview img').hide().fadeIn('slow');
	
	$('body.gallery div.col3 a').click(function() {
		thisclass = $(this).attr("class").toString();
		if (thisclass == 'all') {
			$('#gallery li').fadeIn('slow');
		} else {
			$('#gallery li').hide();
			$('#gallery li.category-'+thisclass).fadeIn('slow');
		}
		return false;
	});
	
	$('body.image #body div.content li').attr('title','Buy this').click(function() {
		var title = $('h2').html();
		location.href='/order/?item='+Url.encode(title+', '+($(this).html()));
	});
	
	if ($('#showcase.col2').length > 0) $.getJSON("http://pipes.yahoo.com/pipes/pipe.run?_id=9c4c8476cbb8cee62f468e5d5170753e&_render=json&_callback=?", function(data){
		var randomitems = new Array();
		
      	$.each(data.value.items, function(i,item){
      		randomitems.push(item);
      	});
      	
      	randomitems.randomize();
      	$.each(randomitems, function(i,item){
      		var src = ('url('+item.link+'/'+item['sl:src']+')'); //__thumb.jpg
        	$("<a/>").attr("href", item.link).attr("title", item.title).css("background-image", src).append("<span>"+item.title+"</span>").appendTo("#showcase.col2");
        	if (i == 9) return false; //limit to top 10
		});
      	
      	showcase();
    });
    
    //populate chosen item
    if ($('body.order').length > 0) {
    	$('#item').val(getQuery('item').replace('&nbsp;',' '));
    }
    
    //check form
    $('body.order form #submit').click(function() {
    	var required = [$('#name'), $('#email'), $('#address'), $('#item')];
    	var valid = true;    	
    	
    	$.each(required, function(i,item){
    		if (item.val() == '') {
    			$(item).addClass("highlight");
    			valid = false;
    		} else {
    			$(item).removeClass("highlight");
    		}
    	});
    	
    	if (!valid) {
    	    alert('Please enter your information.');
    		return false;
    	} else {
    		$('form').attr('action','http://www.gate58art.com/email/');
    		$('form').submit();
    	}    	
    	return false;
    });
    
    //look for images in image content
    alts = $('body.image #body .content img')
    if (alts.length > 0) {
    	alt = $('<ol id="alt" />');
    	//alt.appendTo('#preview');
    	alt.insertBefore(alts[0]);
    	$('<p><b>More images:</b></p>').insertBefore(alt);
    	alts.appendTo(alt).wrap("<li />").addClass('alt').removeAttr('width').removeAttr('height');
    	$('#preview img').addClass('original');
    }
    $('ol#alt li').hover(function(){
    	$('#preview img.original').stop().hide();
    	$('#preview').append($(this).find('img').clone());
    	//.attr('src',$(this).find('img').attr('src')).attr('width','').attr('height','');
    }, function() {
    	$('#preview img.alt').remove();
    	$('#preview img.original').stop().removeAttr('style').hide().fadeIn('slow');
    });
    

});

function getQuery(parameter){
	var p = escape(unescape(parameter));
	var regex = new RegExp("[?&]" + p + "(?:=([^&]*))?","i");
	var match = regex.exec(window.location.search);
	var value = '';
	if (match != null) value = match[1];
	return Url.decode(value);
}

/* array randomize, based on Fisher-Yates shuffle algorithm */
Array.prototype.randomize=function(){var i=this.length;if(i==0)return false;while(--i){var j=Math.floor(Math.random()*(i+1));var tempi=this[i];var tempj=this[j];this[i]=tempj;this[j]=tempi}}

/* URL encode / decode http://www.webtoolkit.info */
var Url={encode:function(string){return escape(this._utf8_encode(string));},decode:function(string){return this._utf8_decode(unescape(string));},_utf8_encode:function(string){string=string.replace(/\r\n/g,"\n");var utftext="";for(var n=0;n<string.length;n++){var c=string.charCodeAt(n);if(c<128){utftext+=String.fromCharCode(c);}
else if((c>127)&&(c<2048)){utftext+=String.fromCharCode((c>>6)|192);utftext+=String.fromCharCode((c&63)|128);}
else{utftext+=String.fromCharCode((c>>12)|224);utftext+=String.fromCharCode(((c>>6)&63)|128);utftext+=String.fromCharCode((c&63)|128);}}
return utftext;},_utf8_decode:function(utftext){var string="";var i=0;var c=c1=c2=0;while(i<utftext.length){c=utftext.charCodeAt(i);if(c<128){string+=String.fromCharCode(c);i++;}
else if((c>191)&&(c<224)){c2=utftext.charCodeAt(i+1);string+=String.fromCharCode(((c&31)<<6)|(c2&63));i+=2;}
else{c2=utftext.charCodeAt(i+1);c3=utftext.charCodeAt(i+2);string+=String.fromCharCode(((c&15)<<12)|((c2&63)<<6)|(c3&63));i+=3;}}
return string;}}