//fgnass.github.com/spin.js
(function(a,b,c){function O(a){H(arguments,function(b,d){a[b]===c&&(a[b]=d)});return a}function N(a){H(arguments,function(b,c){a[m][M(a,b)||b]=c});return a}function M(a,b){var d=a[m],f,g;if(d[b]!==c)return b;b=b.charAt(0).toUpperCase()+b.slice(1);for(g=0;g<E[e];g++){f=E[g]+b;if(d[f]!==c)return f}}function L(a,b){var c=[j,b,~~(a*100)].join("-"),d="{"+j+":"+a+"}",f;if(!F[c]){for(f=0;f<E[e];f++)try{K.insertRule("@"+(E[f]&&"-"+E[f].toLowerCase()+"-"||"")+"keyframes "+c+"{0%{"+j+":1}"+b+"%"+d+"to"+d+"}",K.cssRules[e])}catch(g){}F[c]=1}return c}function J(a,b,c){c&&!c[t]&&J(a,c),a.insertBefore(b,c||null);return a}function I(a){var c=b.createElement(a||"div");H(arguments,function(a,b){c[a]=b});return c}function H(a,b){var c=~~((a[e]-1)/2);for(var d=1;d<=c;d++)b(a[d*2-1],a[d*2])}var d="width",e="length",f="radius",g="lines",h="trail",i="color",j="opacity",k="speed",l="shadow",m="style",n="height",o="left",p="top",q="px",r="childNodes",s="firstChild",t="parentNode",u="position",v="relative",w="absolute",x="animation",y="transform",z="Origin",A="Timeout",B="coord",C="#000",D=m+"Sheets",E="webkit0Moz0ms0O".split(0),F={},G;J(b.getElementsByTagName("head")[0],I(m));var K=b[D][b[D][e]-1],P=function(a){this.opts=O(a||{},g,12,h,100,e,7,d,5,f,10,i,C,j,.25,k,1)},Q=P.prototype={spin:function(b){var c=this,d=c.el=c[g](c.opts);b&&J(b,N(d,o,~~(b.offsetWidth/2)+q,p,~~(b.offsetHeight/2)+q),b[s]);if(!G){var e=c.opts,f=0,i=20/e[k],l=(1-e[j])/(i*e[h]/100),m=i/e[g];(function n(){f++;for(var b=e[g];b;b--){var h=Math.max(1-(f+b*m)%i*l,e[j]);c[j](d,e[g]-b,h,e)}c[A]=c.el&&a["set"+A](n,50)})()}return c},stop:function(){var b=this,d=b.el;a["clear"+A](b[A]),d[t]&&d[t].removeChild(d),b.el=c;return b}};Q[g]=function(a){function s(b,c){return N(I(),u,w,d,a[e]+a[d]+q,n,a[d]+q,"background",b,"boxShadow",c,y+z,o,y,"rotate("+~~(360/a[g]*m)+"deg) translate("+a[f]+q+",0)","borderRadius","100em")}var b=N(I(),u,v),c=L(a[j],a[h]),m=0,r;for(;m<a[g];m++)r=N(I(),u,w,p,1+~(a[d]/2)+q,y,"translate3d(0,0,0)",x,c+" "+1/a[k]+"s linear infinite "+(1/a[g]/a[k]*m-1/a[k])+"s"),a[l]&&J(r,N(s(C,"0 0 4px "+C),p,2+q)),J(b,J(r,s(a[i],"0 0 1px rgba(0,0,0,.1)")));return b},Q[j]=function(a,b,c){a[r][b][m][j]=c};var R="behavior",S="url(#default#VML)",T="group0roundrect0fill0stroke".split(0);(function(){var a=N(I(T[0]),R,S),b;if(!M(a,y)&&a.adj){for(b=0;b<T[e];b++)K.addRule(T[b],R+":"+S);Q[g]=function(){function s(c,e,l){J(k,J(N(h(),"rotation",360/a[g]*c+"deg",o,~~e),J(N(I(T[1],"arcsize",1),d,b,n,a[d],o,a[f],p,-a[d]/2,"filter",l),I(T[2],i,a[i],j,a[j]),I(T[3],j,0))))}function h(){return N(I(T[0],B+"size",c+" "+c,B+z,-b+" "+ -b),d,c,n,c)}var a=this.opts,b=a[e]+a[d],c=2*b,k=h(),m=~(a[e]+a[f]+a[d])+q,r;if(a[l])for(r=1;r<=a[g];r++)s(r,-2,"progid:DXImage"+y+".Microsoft.Blur(pixel"+f+"=2,make"+l+"=1,"+l+j+"=.3)");for(r=1;r<=a[g];r++)s(r);return J(N(I(),"margin",m+" 0 0 "+m,u,v),k)},Q[j]=function(a,b,c,d){d=d[l]&&d[g]||0,a[s][r][b+d][s][s][j]=c}}else G=M(a,x)})(),a.Spinner=P})(window,document)

/**
 * Jquery Scroll Events 1.0.0 - jQuery plugin for special events related to
 * the scrolling action
 * 
 * Require: jQuery 1.3.x
 * Author: Filipi Zimermann / James Padolsey
 * Email:  filipiz at gmail dot com
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
*/

var uid1 = 'D' + (+new Date());
var uid2 = 'D' + (+new Date() + 1);
var uid3 = 'D' + (+new Date() + 2);
var uid4 = 'D' + (+new Date() + 3);

jQuery.event.special.scrollstart = {
	setup: function() {
		var timer;
		var handler =  function(evt) {

			var _self = this;
			var _args = arguments;

			if (timer) {
				clearTimeout(timer);
			} else {
				evt.type = 'scrollstart';
				jQuery.event.handle.apply(_self, _args);
			}

			timer = setTimeout( function(){
					timer = null;
			}, jQuery.event.special.scrollstop.latency);

		};

		jQuery(this).bind('scroll', handler).data(uid1, handler);

	},

    teardown: function(namespaces) {
       jQuery(this).unbind('scroll', jQuery(this).data(uid1) );
    } 
};



jQuery.event.special.scrollstop = {
	latency: 300,
	setup: function() {
		var timer;
		var handler = function(evt) {

			var _self = this;
			var _args = arguments;

			if (timer) {
				clearTimeout(timer);
			}				

			timer = setTimeout( function(){
					timer = null;
					evt.type = 'scrollstop';

					jQuery.event.handle.apply(_self, _args);
			}, jQuery.event.special.scrollstop.latency);

		};

		jQuery(this).bind('scroll', handler).data(uid2, handler);

	},

	teardown: function() {
		jQuery(this).unbind('scroll', jQuery(this).data(uid2) );
	}
};



jQuery.event.special.scrollreachtop = {
    setup: function(data, namespaces) {
    	jQuery(this).bind('scroll', jQuery.event.special.scrollreachtop.handler)
    			.data(uid3, jQuery.event.special.scrollreachtop.handler);
    },

    teardown: function(namespaces) {
        jQuery(this).unbind('scroll', jQuery(this).data(uid3) );
    },

    handler: function(evt) {
    	evt.type = 'scrollreachtop';
        if (parseInt(jQuery(this).scrollTop()) == 0 )
        	jQuery.event.handle.apply(this, arguments);
    } 
};



jQuery.event.special.scrollreachbottom = {
    setup: function(data, namespaces) {
    	jQuery(this).bind('scroll', jQuery.event.special.scrollreachbottom.handler)
    			.data(uid4, jQuery.event.special.scrollreachbottom.handler);
    },

    teardown: function(namespaces) {
        jQuery(this).unbind('scroll', jQuery(this).data(uid4) );
    },

    handler: function(evt) {
		evt.type = 'scrollreachbottom';
        var scrollMaxTop = parseInt(jQuery(this).attr('scrollHeight')) - parseInt(jQuery(this).innerHeight()) - 1;
    	if (parseInt(jQuery(this).scrollTop()) >= scrollMaxTop )
        	jQuery.event.handle.apply(this, arguments);
    } 
};


function getSitePath() {
	trimPath='rw_common/themes/usine/javascript.js';
	sitePath=rwCommonPath.replace(trimPath, '');
	return sitePath;
}

function getRGBbg(element) {
	return jQuery(element).css('background-color');
}

function addBGAlpha(element,alpha) {
	var rgbVal=getRGBbg(element);
	rgbVal=rgbVal.replace(/\s/g, "");
	var rgbaVal=rgbVal.substring(0,rgbVal.length-1)+','+alpha+')';
	rgbaVal=rgbaVal.replace('rgb', 'rgba');
	jQuery(element).css('background-color', rgbaVal);
}

function fixLinks(theURL) {
	
	var theSitePath=getSitePath();
	jQuery('#content a').each(function() {
		var oURL=jQuery(this).attr('href');
		oURL=oURL.replace(theSitePath, '');
		if (!(/http/.test(oURL))) {
			var nURL=theURL+"/../"+oURL;
			jQuery(this).attr('href', nURL);
		}
		if (jQuery(this).attr('rel')=='external') {
			jQuery(this).attr('target', '_blank');
			}
	});
	jQuery('#contentContainer img').each(function() {
		
		
		var oIURL=jQuery(this).attr('src');
		oIURL=oIURL.replace(theSitePath, '');
		var nIURL=theURL+"/../"+oIURL;
		jQuery(this).attr('src', nIURL);
	});
	return;
}

function initSB() {
	
	Shadowbox.init({
		//players: ["html","img","swf","flv","qt","iframe"]
		skipSetup: true
	});
	
	
}

function setUpSB() {
	jQuery('#contentContainer a[rel^="shadowbox"]').each(function() {
		Shadowbox.addCache(jQuery(this)[0]);
		
		
	});
}

function setUpPhotoAlbumSB() {
	/*Shadowbox.init({
		//players: ["html","img","swf","flv","qt","iframe"],
		skipSetup: true
	});*/
	jQuery('div.thumbnail-frame a').each(function() {
		var theOURL=jQuery(this).attr('href');
		var theNURL=theOURL.replace(/\.html$/gi, '.jpg');
		var theTitle=jQuery(this).next('p.thumbnail-caption').text();
		jQuery(this).attr({
			'href': theNURL,
			'rel': 'shadowbox[photoSet]',
			'title': theTitle
		});
	});
	Shadowbox.setup('div.thumbnail-frame a', {
			player: 'img'
			});
	
}

function addToFooter() {
	if (jQuery('#content #addToFooter').length==0) {return;}
	jQuery('#footer').prepend('<div id="importedFooter">'+jQuery('#addToFooter').html()+'</div>');
	jQuery('#content #addToFooter').empty();
	return;
}


function loadPage(theURL) {
	
	var spinOpts = {
		lines: 12, // The number of lines to draw
		length: 7, // The length of each line
		width: 5, // The line thickness
		radius: 10, // The radius of the inner circle
		color: '#ccc', // #rbg or #rrggbb
		speed: 1, // Rounds per second
		trail: 100, // Afterglow percentage
		shadow: true // Whether to render a shadow
	};
	
	if (!(jQuery.browser.msie  && parseInt(jQuery.browser.version) == 7)) {
  		jQuery('div#overlay').delay(500).fadeOut(300);
	  }
		
	//if (!jQuery.browser.msie) {
		jQuery('body').append('<div id="loadingSpin"></div>');
		var spinner = new Spinner(spinOpts).spin(document.getElementById('loadingSpin'));
		jQuery('#loadingSpin').fadeIn(500);
	//}
	
	jQuery.get(theURL, function(htmlText) {
		if (jQuery(htmlText).find('div#loadiFrame').length>0) {
			//if(/Firefox/.test(navigator.userAgent)) {
				jQuery('#content').css('height', '90%');
			//	}
			jQuery('#contentContainer #content').empty().html('<iframe src="'+theURL+'"id="iframeWrapper" frameborder="0" height="100%" width="100%" style="width: 100%; height: 100%;border: 0" />');
			jQuery('#content iframe').load(function() {
				
				//jQuery(this).css('height',theScrollHeight);
				//this.height=this.contentWindow.document.body.offsetHeight + 50 + 'px';
				jQuery('div#contentContainer').delay(300).fadeIn('slow');
				if (!jQuery('div#contentContainer div.closeButton').length>0) {
						jQuery('div#contentContainer').append('<div class="closeButton">X</div>');
						}
				jQuery('div#loadingSpin').fadeOut(200).remove();
				spinner.stop();
				//setUpPhotoAlbumSB();
				//initSB();
				
				
			});
		}
		
		
		else {
			if(jQuery(htmlText).find('#content').length>0) {
				
				var theFullLink=theURL + ' #content';
				//jQuery('#contentContainer #content').remove();
		
				jQuery('div#contentContainer').empty().load(theFullLink, function() {
				
					jQuery('div#contentContainer').delay(300).fadeIn('slow');
					if (!jQuery('div#contentContainer div.closeButton').length>0) {
						jQuery('div#contentContainer').append('<div class="closeButton">X</div>');
						}
					jQuery('div#loadingSpin').fadeOut(200).remove();
					spinner.stop();
					fixLinks(theURL);
					if (jQuery('div.thumbnail-frame a').length>0) {
						
						setUpPhotoAlbumSB();
					}
					else if (jQuery('#contentContainer a[rel^="shadowbox"]').length>0) {
						//initSB();
						setUpSB();
					}
				});
			}
			else {
			jQuery('div#loadingSpin').fadeOut(200).remove();
					spinner.stop();
			}
		}
		});
}

function fixRC() {
	if(jQuery('#rcCart').length>0) {
		jQuery('#contentContainer').css({
			'height':'80%',
			'top':'10%'
		});
	}
	return;
}



function checkiPhone() {
	var iPhone=((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)));
	return iPhone;
}

function checkiPad() {
	var iPad=navigator.userAgent.match(/iPad/i);
	return iPad;
}

function toggleContent() {
	jQuery('body').prepend('<button type="button" class="showContent">Show Content</button>');
    jQuery('button.showContent').toggle(function() {
    	jQuery('#contentContainer').fadeIn('fast');
	    jQuery(this).html('Hide Content');
	    }, function() {
	    jQuery('#contentContainer').fadeOut('fast');
	    jQuery(this).html('Show Content');
	   });	
}


jQuery(document).ready(function(){
	//sitePath=getSitePath();
	iPhone=checkiPhone();
	iPad=checkiPad();
	addToFooter();
	initSB();
	jQuery('#content a').each(function() {
		if (jQuery(this).attr('rel')=='external') {
			jQuery(this).attr('target', '_blank');
			}
	});
		
	jQuery('div#contentContainer').append('<div class="closeButton">X</div>');
	jQuery('#contentContainer').bind('scrollstart', function() {
		jQuery('div.closeButton').fadeOut(100);
	});
	
	jQuery('#contentContainer').bind('scrollstop', function() {
		jQuery('div.closeButton').css('margin-top', (jQuery(this).scrollTop() + 0) + "px");
		jQuery('div.closeButton').fadeIn(1000);
	});
	jQuery('ul.nav ul').removeClass('nav');
	jQuery.fx.interval = 60;
	jQuery.ajaxSettings.cache = false;
	
	jQuery('#contentContainer').css('display', 'none');
	addBGAlpha('ul.nav', '0.8');
	addBGAlpha('#contentContainer', 0.8);
	
	jQuery('#container img').click(function() {
		
		jQuery('#container ul.nav').fadeIn('slow');
		if (!(jQuery.browser.msie  && parseInt(jQuery.browser.version) == 7)) {
  			jQuery('div#overlay').fadeTo(600, 0.8);
	  	}
		
	});
	jQuery('#container ul.nav').mouseleave(function() {
		jQuery(this).delay(1000).fadeOut('fast');
		if (!(jQuery.browser.msie  && parseInt(jQuery.browser.version) == 7)) {
  			jQuery('div#overlay').delay(1200).fadeOut(300);
	  	}
		
	});
	jQuery('#container ul.nav').mouseover(function() {
		//jQuery('.box').stop().fadeTo(300,0.2);
		
	});
	
	jQuery('#container ul.nav a').click(function(event) {
		jQuery('#container ul.nav').delay(200).fadeOut('fast');
		
		var linkToLoad=jQuery(this).attr('href');
		
		if (jQuery(this).attr('rel')=='external') {return true;}
		
		
		jQuery('#container ul.nav li').removeClass('current');
		jQuery(this).parent('li').addClass('current');
		//if (!jQuery(this).parent('li').hasClass('current')) {
			loadPage(linkToLoad);
		//}
			
		/*else {
			jQuery('div#contentContainer').delay(300).fadeIn('slow');
			
		}*/
		
		return false;
	});
	
	jQuery('#contentContainer div.closeButton').live('click',function() {
		Shadowbox.clearCache();
		jQuery('#contentContainer').fadeOut('fast');
		jQuery('#container ul.nav').fadeIn('slow');

		jQuery('#container img').trigger('click');
	
	
	});
	
	
	jQuery('#content a').live('click', function(e) {
		var theURL=jQuery(this).attr('href');
		var theRel=jQuery(this).attr('rel');
		var theTarget=jQuery(this).attr('target');
		
		
		
		
		if (theTarget=='_blank' || theTarget=='_parent' || theRel=='external' || (/^http:/.test(theURL)) || (!(/.html$/.test(theURL)))) {
			return true;
			}
		
		else {
			loadPage(theURL);
			e.preventDefault();
			return false;
		}
	});
	
	
	
	if (window.PIE) {
        jQuery('#container img').add('ul.nav').add('div#overlay').add('#contentContainer').add('#contentContainer div.closeButton').add('#container h1').add('#container h2').each(function() {
            PIE.attach(this);
        });
    }
    
    if(/^file:\/\/\/private\/var\/folders/.test(document.location)) {
    	toggleContent();
    }
    
    
    
    
});

jQuery(window).load(function() {
	fixRC();
});
