/*
    This file is part of JonDesign's SmoothSlideshow v2.0.

    JonDesign's SmoothSlideshow is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    JonDesign's SmoothSlideshow is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Main Developer: Jonathan Schemoul (JonDesign: http://www.jondesign.net/)
    Contributed code by:
    - Christian Ehret (bugfix)
    - Simon Willison (addLoadEvent)
*/

// declaring the class
function getRandom(min, max)
{		
	var randomNum = Math.random() * (max-min); 

// Round to the closest integer and return it
	return(Math.round(randomNum) + min); 
}


var timedSlideShow = Class.create();

// implementing the class
timedSlideShow.prototype = {
	initialize: function(element, data) {
		this.currentIter = 0;
		this.lastIter = 0;
		this.maxIter = 0;
		this.slideShowElement = element;
		this.slideShowData = data;
		this.slideShowInit = 1;
		this.slideElements = Array();
		this.slideShowDelay = 16000;
		this.articleLink = "";
		this.slideInfoZone = "";

		element.style.display="block";

		this.articleLink = document.createElement('a');
		this.articleLink.className = 'global';
		this.articleLink.target = 'top';
		element.appendChild(this.articleLink);
		this.articleLink.href = "";

		this.maxIter = data.length;
		for(i=0;i<data.length;i++)
		{
			var currentImg = document.createElement('div');
			
			currentImg.className = "slideElement";
			currentImg.style.position="absolute";
			currentImg.style.left="0px";
			currentImg.style.top="0px";
			currentImg.style.margin="0px";
			currentImg.style.border="0px";
			
			var currentLink = document.createElement('a');
			currentLink.className = 'global';
			currentLink.target = 'top';
			currentLink.href = data[i][1];
			currentLink.title = data[i][2];
			
			var cImg = new Image();
			cImg.src = data[i][0];
			cImg.width = data[i][4];
			cImg.height = data[i][5];
			cImg.style.width = data[i][4];
			cImg.style.height = data[i][5];
			
			currentLink.appendChild(cImg);
			currentImg.appendChild(currentLink);
			element.appendChild(currentImg);
			
			currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 1000});
			currentImg.setStyle('opacity',0);
			this.slideElements[parseInt(i)] = currentImg;
		}
		
		this.loadingElement = document.createElement('div');
		this.loadingElement.className = 'loadingElement';
		this.articleLink.appendChild(this.loadingElement);
		
		/*this.slideInfoZone = document.createElement('div');
		this.slideInfoZone.className = 'slideInfoZone';
		this.articleLink.appendChild(this.slideInfoZone);
		this.slideInfoZone.style.opacity = 0;*/

		this.doSlideShow();
	},
	destroySlideShow: function(element) {
		var myClassName = element.className;
		var newElement = document.createElement('div');
		newElement.className = myClassName;
		element.parentNode.replaceChild(newElement, element);
	},
	startSlideShow: function() {
		this.loadingElement.style.display = "none";
		this.lastIter = 0;
		this.currentIter = getRandom(0, this.maxIter-1);
		this.slideShowInit = 0;
		this.slideElements[parseInt(this.currentIter)].setStyle('opacity', 1);
		//setTimeout(this.showInfoSlideShow.bind(this),1000);
		//setTimeout(this.hideInfoSlideShow.bind(this),this.slideShowDelay-1000);
		setTimeout(this.nextSlideShow.bind(this),this.slideShowDelay);
	},
	nextSlideShow: function() {
		this.lastIter = this.currentIter;
		//this.currentIter++;
        this.currentIter = getRandom(0, this.maxIter-1);
        
        while (this.lastIter == this.currentIter){
            this.currentIter = getRandom(0, this.maxIter-1);
        }
        
		if (this.currentIter >= this.maxIter)
		{
            alert(this.currentIter+ " " + this.maxIter);
			this.currentIter = 0;
			this.lastIter = this.maxIter - 1;
		}
		this.slideShowInit = 0;
		this.doSlideShow.bind(this)();
	},
	doSlideShow: function() {
		if (this.slideShowInit == 1)
		{
			imgPreloader = new Image();
			// once image is preloaded, start slideshow
			imgPreloader.onload=function(){
				setTimeout(this.startSlideShow.bind(this),10);
			}.bind(this);
			imgPreloader.src = this.slideShowData[0][0];
		} else {
			if (this.currentIter != 0) {
				this.slideElements[parseInt(this.currentIter)].currentOpacity.options.onComplete = function() {
					this.slideElements[parseInt(this.lastIter)].setStyle('opacity',0);
				}.bind(this);
				this.slideElements[parseInt(this.currentIter)].currentOpacity.custom(0, 1);
			} else {
				this.slideElements[parseInt(this.currentIter)].setStyle('opacity',1);
				this.slideElements[parseInt(this.lastIter)].currentOpacity.custom(1, 0);
			}
			//setTimeout(this.showInfoSlideShow.bind(this),1000);
			//setTimeout(this.hideInfoSlideShow.bind(this),this.slideShowDelay-1000);
			setTimeout(this.nextSlideShow.bind(this),this.slideShowDelay);
		}
	},
	showInfoSlideShow: function() {
		this.articleLink.removeChild(this.slideInfoZone);
		this.slideInfoZone = document.createElement('div');
		this.slideInfoZone.styles = new fx.Styles(this.slideInfoZone);
		this.slideInfoZone.setStyle('opacity',0);
		var slideInfoZoneTitle = document.createElement('h2');
		slideInfoZoneTitle.innerHTML = this.slideShowData[this.currentIter][2]
		this.slideInfoZone.appendChild(slideInfoZoneTitle);
		var slideInfoZoneDescription = document.createElement('p');
		slideInfoZoneDescription.innerHTML = this.slideShowData[this.currentIter][3];
		this.slideInfoZone.appendChild(slideInfoZoneDescription);
		this.articleLink.appendChild(this.slideInfoZone);
		this.articleLink.href = this.slideShowData[this.currentIter][1];
		this.slideInfoZone.className = 'slideInfoZone';
		this.slideInfoZone.normalHeight = this.slideInfoZone.getStyle('height', true).toInt();
		this.slideInfoZone.styles.custom({'opacity': [0, 0.7], 'height': [0, this.slideInfoZone.normalHeight]});
	},
	hideInfoSlideShow: function() {
		this.slideInfoZone.styles.custom({'opacity': [0.7, 0]});
	}
};

function initTimedSlideShow(element, data) {
	var slideshow = new timedSlideShow(element, data);
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}





var slideimages=new Array()
var slidelinks=new Array()
function slideshowimages(){
for (i=0;i<slideshowimages.arguments.length;i++){
slideimages[i]=new Image()
slideimages[i].src=slideshowimages.arguments[i]
}
}

function slideshowlinks(){
for (i=0;i<slideshowlinks.arguments.length;i++)
slidelinks[i]=slideshowlinks.arguments[i]
}

function gotoshow(){
if (!window.winslide||winslide.closed)
winslide=window.open(slidelinks[whichlink])
else
winslide.location=slidelinks[whichlink]
winslide.focus()
}


/* CLICK TRACKING VARIABLES */

var clicktracker_url        = "/click-tracker.php";
var clicktracker_domains    = Array("","www.motorcyclesupermarket.com","motorcyclesupermarket.com");
var clicktracker_extensions = Array();


/* CLICK TRACKING SCRIPTS */


// Copyright (C) 2005-2008 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
//
// YOU MAY NOT
// (1) Remove or modify this copyright notice.
// (2) Re-distribute this code or any part of it.
//     Instead, you may link to the homepage of this code:
//     http://www.php-development.ru/php-scripts/click-tracker.php
// (3) Use this code as a part of another product.
//
// YOU MAY
// (1) Use this code on your website.
//
// NO WARRANTY
// This code is provided "as is" without warranty of any kind.
// You expressly acknowledge and agree that use of this code is at your own risk.


// ***** Aux *******************************************************************

// ***** clicktracker_inarray  *****

function clicktracker_inarray (arr, val)
{
  for (var i in arr) if (arr[i] == val) return true;
  return false;
}

// ***** clicktracker_innertxt *****

function clicktracker_innertxt(str)
{
  str = str.replace(/<[^>]*>/g, ' ');
  str = str.replace(  /&amp;/g, '&');
  str = str.replace( /&nbsp;/g, ' ');
  str = str.replace(   /^\s+/g,  '');
  str = str.replace(   /\s+$/g,  '');
  return str;
}


// ***** URL *******************************************************************

var clicktracker_re_scheme = "^\\w+://";
var clicktracker_re_folder = "((?:-|\\w|\\.)*)";
var clicktracker_re_domain = clicktracker_re_scheme+       clicktracker_re_folder;
var clicktracker_re_urlall = clicktracker_re_domain+"(?:/"+clicktracker_re_folder+')*';

// ***** clicktracker_domain *****

function clicktracker_domain(url)
{
  var reg   = new RegExp(clicktracker_re_domain);
  var match = reg.exec(url);
  if (!match) return "";
  match = match[match.length-1];
  return match;
}

// ***** clicktracker_extension *****

function clicktracker_extension(url)
{
  var reg   = new RegExp(clicktracker_re_urlall);
  var match = reg.exec(url);
  if (!match) return "";
  match = match[match.length-1].split(".");
  return (match.length >= 2) ? match[match.length-1] : "";
}


// ***** Track *****************************************************************

// ***** clicktracker_aux *****

function clicktracker_aux(url, title)
{
var theUrl = "";
try{
  var img = new Image();
  theUrl = clicktracker_url+"?url="+url+"&title="+title+"&rand="+Math.random();
  img.src = theUrl;
  }
  catch(ex)
  {
  //alert("exception");
  }
  
  /*xmlHttp = GetXmlHttpObject();
  //alert(xmlHttp);
  xmlHttp.open("get",theUrl,true);
  xmlHttp.send(null);  
  //alert(theUrl);
*/  
}

// ***** clicktracker *****

function clicktracker(e)
{
  var ie  = navigator.appName == "Microsoft Internet Explorer";
  var src = ie ? window.event.srcElement : e.target;
  var tag =  (src.tagName.toLowerCase() != "a") ? src.parentNode : src;

  if (!tag || tag.tagName.toLowerCase() != "a") return;

  domain    = clicktracker_domain   (tag.href);
  extension = clicktracker_extension(tag.href);

  if ( clicktracker_inarray(clicktracker_domains, domain) &&
      !clicktracker_inarray(clicktracker_extensions, extension)) return;

  var url   = tag.href;
  var title = '';

  if (!title) if (tag.tagName.toLowerCase() ==   "a") title = clicktracker_innertxt(tag.innerHTML);
  if (!title) if (tag.tagName.toLowerCase() ==   "a") title = clicktracker_innertxt(tag.title);
  if (!title) if (src.tagName.toLowerCase() == "img") title = clicktracker_innertxt(src.alt);
  if (!title) if (src.tagName.toLowerCase() == "img") title = clicktracker_innertxt("Image");
  url   = escape(url  .substr(0, 150));
  title = escape(title.substr(0, 150));

  if (url && title) setTimeout("clicktracker_aux('"+url+"', '"+title+"')", 10);
  return;
}


// ***** Attach Events *********************************************************

if (navigator.appName == "Microsoft Internet Explorer"){
     document.attachEvent   ('onclick', clicktracker);
     }
else{ document.addEventListener('click', clicktracker, false);
}

