/***************************************
    GENERAL SCRIPTS
***************************************/

/* jQMinMax v0.1 - Copyright (c) 2006 Dave Cardwell (http://davecardwell.co.uk/)
   Released under the MIT License (http://www.opensource.org/licenses/mit-license.php) */
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[(function(e){return d[e]})];e=(function(){return'\\w+'});c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('u m(){$.5={D:y,v:y};$(G).12(m(){8 h=G.W(\'I\');$(h).e({\'3\':\'J\',\'6-3\':\'K\'});$(\'L\').M(h);$.5.v=(h.s&&h.s==2);$(h).N();b($.5.v)o;$.5.D=O;$.5.A();$(\':5\').5()});$.5.A=m(){8 p=u E(\'6-3\',\'6-4\',\'9-3\',\'9-4\');8 5=u E();Q(8 i=0;i<p.R;i++){8 n="$.e(a,\'"+p[i]+"\')!=\'S\'&&"+"$.e(a,\'"+p[i]+"\')!=\'z\'&&"+"$.e(a,\'"+p[i]+"\')!=f.g";b(p[i].U(2)==\'x\')n+="&&$.e(a,\'"+p[i]+"\')!=\'X\'";$.n[\':\'][p[i]]=n;5[i]=\'(\'+n+\')\'}$.n[\':\'][\'5\']=5.Y(\'||\')};$.Z.5=m(){o $(c).10(m(){8 7={\'6-3\':r(c,\'6-3\'),\'9-3\':r(c,\'9-3\'),\'6-4\':r(c,\'6-4\'),\'9-4\':r(c,\'9-4\')};8 3=c.s;8 4=c.w;8 k=3;8 l=4;b(7[\'9-3\']!=f.g&&k>7[\'9-3\'])k=7[\'9-3\'];b(7[\'6-3\']!=f.g&&k<7[\'6-3\'])k=7[\'6-3\'];b(7[\'9-4\']!=f.g&&l>7[\'9-4\'])l=7[\'9-4\'];b(7[\'6-4\']!=f.g&&l<7[\'6-4\'])l=7[\'6-4\'];b(k!=3)$(c).e(\'3\',k);b(l!=4)$(c).e(\'4\',l)})};m r(t,p){8 q=$(t).e(p);b(q==f.g||q==\'z\')o f.g;8 j;j=q.B(/^\\+?(\\d*(?:\\.\\d+)?)%$/);b(j){o T.V(C((/3$/.h(p)?$(t).F().H(0).s:$(t).F().H(0).w)*j[1]/P))}j=q.B(/^\\+?(\\d*(?:\\.\\d+)?)(?:11)?$/);b(j){o C(j[1])}o f.g}}();',62,65,'|||width|height|minmax|min|constraint|var|max||if|this||css|window|undefined|test||result|newWidth|newHeight|function|expr|return||raw|calculate|offsetWidth|obj|new|native|offsetHeight||false|auto|expressions|match|Number|active|Array|parent|document|get|div|1px|2px|body|append|remove|true|100|for|length|0px|Math|charAt|round|createElement|none|join|fn|each|px|ready'.split('|'),0,{}))


/*****************************/
/**** menu                 ***/
/*****************************/	
$.fn.fadeMenu = function () {
    return this.hover(function(){
            $(this).find('> ul', this).css({visibility: "visible"});//,display: "none"}).fadeIn(135);
        },function(){
            $(this).find('> ul', this).css({visibility: "hidden"})//do like the link animation so it can slide up too and use the animation - have a function that this and animageLink can use...
        ;
    });
};


/***************************************
    ON DOCUMENT LOAD
    ------------------------------------
    Any code between this function
    executs when the page had loaded
    fully.
***************************************/
$(document).ready(function()
{     
    //menu
	$("#globalnav li").fadeMenu(); 
	
	//ie6 max-width fix
	$('h2.promotitle').minmax();	

	//http://www.no-margin-for-errors.com/projects/prettyPhoto/
	$("a[rel^='prettyPhoto']").prettyPhoto();
		
	//product lightbox
	$(".image1 a").prettyPhoto();
	
	
	
	
	
	
	/***********************************************/
	/**** display large image on product detail  ***/
	/***********************************************/
	$(".images a").click(function(e)
	{		
		var index = $(".images a").index(this);

		e.preventDefault();
		
		$(".image1 img").attr("src", $(this).attr("href"))		
						//setup lightbox with new image with the images parent link
						.parent().attr("href", $(this).attr("class"));		
		
		//window.location.hash = "#image"+(index+1); //put the image here		
		
	});	
	//todo
/*	if(window.location.hash) {
			///depnding on hash trigger the imge...
	}	
	*/

	
	/*****************************/
	/**** display large fade  ***/  
	/****************************	
	$(".images a").click(function(e)
	{	
		e.preventDefault();
		
		var newSrc = $(this).attr("href"),
			$newImage =  $("<img src='"+newSrc+"' />"),
			$oldImage =  $(".image1 .img");
			
		$oldImage.stop().css("opacity", "1");
			
		$newImage.hide();
		$newImage.css({"position": "absolute"});
		$(".image1").append($newImage);
		
		$newImage.fadeIn("slow");
		
		$oldImage.fadeOut("slow").remove();
		
		
		
		//window.location.hash = "#test";
	});
	*/
	
	
	/*****************************/
	/**** error page           ***/
	/*****************************/	
	var $yahoo = $("#yahoosearch"),
		$output = $("#yahoosearchoutput");

	//developer.yahoo.com/search/boss/ 
	//http://developer.yahoo.com/common/json.html
	
	
	$("#error input.button").click(function(e) {
		$yahoo.css("background", "url(images/search_loading.gif) center center no-repeat");
		$output.html("");
		
		var query = $yahoo.val();
		var path  = "http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query="
					+query+"+site:shadesofcomfort.com&results=10&output=json";		
        $.ajax({
            url:       path,
            dataType:  'jsonp',
            success:   ws_results                	    
		}); 
	
	});
	

	function ws_results(data) {
		
		
		$output.empty();
		var r =  data.ResultSet;
		//log($yahoo, data);
		
		var html = "", 
			end = "";
		//html+="<p>Displaying the first " + parseInt(r.firstResultPosition) + " - " + (parseInt(r.firstResultPosition) + parseInt(r.totalResultsReturned)) + " from " + r.totalResultsAvailable + " results.</p><ul>";
		
		
		if (r.totalResultsAvailable == 0) {
			html="<p>Sorry, there are no results available for this query. Please try again.</p>";
		} else {
			html+="<p>Displaying " + r.totalResultsReturned + " results:</p><ul>";
			
			$.each(r.Result, function(i, result) {	
				
				if( i === r.totalResultsReturned-1) end = " class='last'";
				
				html += "<li" + end + "><h3><a href='"+result.Url+"'>"+result.Title+"</a></h3><p>"+result.Summary+"</p></li>";     
				if ( i === 9 ) return false;
			});		
	        
			html+="</ul>";         
        }      
        
        
        $output.append(html);        
		$yahoo.css("background", "");        
	}
	
});

function log() {
    try {   
        var args = [];
        for(var i=0; i<arguments.length;i++)
            args.push("arguments["+i+"]");
            
        eval("console.log("+args.join(",")+")");
           
    }catch(e){}
}