/******************************************************************************
Name:    Random Script
Version: 1.0.0 (April 8, 2010)
Author:  Your Store Wizards
Support: http://www.yourstorewizards.com

License:

This code may not be distributed or reused without written permission from 
Your Store Wizards.

******************************************************************************/

if (!yswRandom) {
	var yswRandom  = {
		storeID : '',
		src : '',
		items : {},
		randOrd : function(a, b) { 
			return 0.5 - Math.random();
		},
		getScriptLocation : function() {
			if (!this.src) {
				var scripts = document.getElementsByTagName("script");
				var src = scripts[scripts.length-1].src;
				var scriptfile = src.split('/');
				if (scriptfile.length > 1) {
					this.src = src.replace(scriptfile[scriptfile.length-1],'');
				}
			}
		},
		activate : function() {
			for(var currArea in yswRandom.items) {
				yswRandom.items[currArea].sort( this.randOrd );
				var randArea = jQuery("#yswRand" + currArea);
				if (randArea.length == 1) {
					// if there is a random area
					//change image links
					jQuery(".rand" + currArea + "Image", randArea).each(function(index) {
						if(yswRandom.items[currArea][index]) {
							if (jQuery(this).attr('href')) {
								// if it's a link
								jQuery(this).attr('href',yswRandom.items[currArea][index][0]);
							}
							if (jQuery(this).attr('src')) {
								//if it's the image
								jQuery(this).attr('src',yswRandom.items[currArea][index][1]).removeAttr('width').removeAttr('height');
							}
							jQuery("img", jQuery(this)).attr('src',yswRandom.items[currArea][index][1]).removeAttr('width').removeAttr('height');
							jQuery("a", jQuery(this)).attr('href',yswRandom.items[currArea][index][0]);
						} else {
							jQuery(this).remove();
						}
					});
					
					//change text links
					jQuery(".rand" + currArea + "Name", randArea).each(function(index) {
						if(yswRandom.items[currArea][index]) {
							if (jQuery(this).attr('href')) {
								// if it's a link
								jQuery(this).attr('href',yswRandom.items[currArea][index][0]);
								jQuery(this).html(yswRandom.items[currArea][index][2]);
							} else {
								jQuery("a", jQuery(this)).html(yswRandom.items[currArea][index][2]);
								jQuery("a", jQuery(this)).attr('href',yswRandom.items[currArea][index][0]);
							}
						} else {
							jQuery(this).remove();
						}
					});
					//change abstract links
					jQuery(".rand" + currArea + "Abstract", randArea).each(function(index) {
						if(yswRandom.items[currArea][index]) {
							if (jQuery(this).attr('href')) {
								// if it's a link
								jQuery(this).attr('href',yswRandom.items[currArea][index][0]);
								jQuery(this).html(yswRandom.items[currArea][index][3]);
							} else {
								jQuery("a", jQuery(this)).html(yswRandom.items[currArea][index][3]);
								jQuery("a", jQuery(this)).attr('href',yswRandom.items[currArea][index][0]);
							}
						} else {
							jQuery(this).remove();
						}
					});
				}
			}
		},
	}
	yswRandom.getScriptLocation();
}

//yswRandom.items['items'] = new Array();
//yswRandom.items['items'][0] = ['http://www.shower-curtains.com', 'http://www.shower-curtains.com/lib/showercurtains/header-image.gif', 'shower curtains'];
//yswRandom.activate();
