﻿function addEvent(obj, evType, fn){ 
	
    if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); //last param false if you want the ie buble b
      return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
    return r; 
 } else { 
   return false; 
 } 
}

//add string functions
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function getElementsByClass(searchClass,node,tag) {
  var classElements = new Array();
  if (node == null)
    node = document;
  if (tag == null)
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if (pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

///-function-//////////////////////////////////////////////////////////////////
// wGet
///////////////////////////////////////////////////////////////////////////////
function wGet(ID) { return window.document.getElementById(ID); }


///-class-/////////////////////////////////////////////////////////////////////
// Vector2D
///////////////////////////////////////////////////////////////////////////////
function Vector2D(X, Y)
{
    ///-data-//////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////

    this._x = X;
    this._y = Y;


    ///-arithmetic-////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////

    this.AddVal = function(Value) { this._x += Value; this._y += Value; return this; };
    this.SubVal = function(Value) { this._x -= Value; this._y -= Value; return this; };
    this.MulVal = function(Value) { this._x *= Value; this._y *= Value; return this; };
    this.DivVal = function(Value)
    {
        if (Value)
        {
            this._x /= Value;
            this._y /= Value;
        }

        return this;
    };


    this.AddVec = function(Vector) { this._x += Vector._x; this._y += Vector._y; return this; };
    this.SubVec = function(Vector) { this._x -= Vector._x; this._y -= Vector._y; return this; };
    this.MulVec = function(Vector) { this._x *= Vector._x; this._y *= Vector._y; return this; };
    this.DivVec = function(Vector)
    {
        if (Vector._x && Vector._y)
        {
            this._x /= Vector._x;
            this._y /= Vector._y;
        }

        return this;
    };


    ///-methods-///////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////

    this.toString = function() { return this._x + "|" + this._y; };
    this.Clone = function() { return new Vector2D(this._x, this._y); };

    this.GetXInt = function() { return Math.round(this._x); };
    this.GetYInt = function() { return Math.round(this._y); };

    this.GetXPxl = function() { return Math.round(this._x) + 'px'; };
    this.GetYPxl = function() { return Math.round(this._y) + 'px'; };
}

///-class-/////////////////////////////////////////////////////////////////////
// FlyItem
///////////////////////////////////////////////////////////////////////////////
function FlyItem(Parent,Item)
{
    ///-data-//////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////
 
    this._parent = Parent;
    this._ID = this._parent._ID + 'Item' + this._parent._items.length;
    this._element = Item;
    this._position = this._parent._itemPosition.Clone(); //new Vector2D(0, 0);
    this._size = this._parent._itemSize.Clone();
    this._index = this._parent._items.length;
	this._deltay = 0; //Math.floor(Math.random() * 12);
	this._deltax = 0; //Math.floor(Math.random() * 12);
	this._xdir = 1; //(Math.floor(Math.random() * 2) == 1) ? '+' : '-';
	this._ydir = 1; //Math.floor(Math.random() * 2) == 1) ? '+' : '-';
	this._counter = 0;
	this._speed = 0;
	this._opacity = 100;
	this._opacityStep = 3;
	this._freeze = true;

    var This = this;
    ///-methods-///////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////
	
    this.GetElement = function()
    {
        if (!this._element)
            this._element = wGet(this._ID);
        return this._element;
    };
	
	this.Reset = function()
	{
		this._position = this._parent._itemPosition.Clone(); 
		this._size = this._parent._itemSize.Clone();
		this._xdir = (Math.floor(Math.random() * 2) == 1) ? 1 : -1;
		this._ydir = (Math.floor(Math.random() * 2) == 1) ? 1 : -1;
		this._deltay = Math.floor(Math.random() * 90);
		this._deltax = Math.floor(Math.random() * 90);
		this._counter = 1;
		this._opacity = 100;
		this._opacityStep = 3 ;
		this._freeze = true;
		this._parent._unfreezedItems--
		this.StyleUpdate();
	};

    this.Initialize = function()
	{
		this.GetElement().id = this._ID;
		this._parent._unfreezedItems++;
		this.Reset();
		//addEvent(this._element,"click",function(e){alert("clicked");});
    };
	
	this.ChangeOpacity = function()
	{
	    this._element.style.opacity = (this._opacity / 100); 
		this._element.style.MozOpacity = (this._opacity / 100); 
		this._element.style.KhtmlOpacity = (this._opacity / 100); 
		this._element.style.filter = "alpha(opacity=" + this._opacity + ")";
		this._opacity = this._opacity - this._opacityStep;	
	};

	this.StyleUpdate = function()
	{
			if (this.GetElement())
			{
				this._element.style.left = this._position.GetXPxl();
				this._element.style.top = this._position.GetYPxl();
				this._element.style.width = this._size.GetXPxl();
				this._element.style.height = this._size.GetYPxl();
				this._element.style.zIndex = 999;
				this._element.style.display = 'inline-block';
			}
			if(this._opacityStep != 0) {this.ChangeOpacity();}
	};
	
    this.Update = function()
    {
		if(!this._freeze){
			this._position._x = this._position._x  + (this._deltax +this._counter)/18 * this._xdir;
			this._position._y = this._position._y + (this._deltay +this._counter)/18 * this._ydir;
			this._size._x = this._counter * 3;
			this._size._y = this._counter * 3;
			if ((this._position._x + this._size._x >= this._parent._size._x )||(this._position._y + this._size._y >= this._parent._size._y) || (this._position._x < 0)||(this._position._y < 0)) {
				this.Reset();
				//this._parent._updateItems++;
				//this._freeze = true;		
			} else {
				this.StyleUpdate();
				this._counter++;
			}
		}

    };

 }


///-class-/////////////////////////////////////////////////////////////////////
// Fly
///////////////////////////////////////////////////////////////////////////////
function Fly(ID, LayerID, Speed, infoid, ImagesSrc)
{
    ///-data-//////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////

    this._ID = ID;
    this._layerID = LayerID;
    this._interval = '';
    this._items = new Array();
	this._position = new Vector2D(0,0);
	this._size = new Vector2D(0,0);
    this._itemSize = new Vector2D(0,0);
    this._itemPosition = new Vector2D(0,0);
    this._clickedItem = 0;
	this._speed = Speed;
	this._maxItems = 10;
	this._updateItems = 0;
	this._unfreezedItems = 0;
	this._lastFreezed = 0;
	this._infoID = infoid;
	this._info = null;
	this._imagesSrc = ImagesSrc;
	this._ini = false;

    var This = this;
	var flyspeed = this._speed;
    ///-methods-///////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////

    this.AddItem = function(Item)
    {

        this._items[this._items.length] = new FlyItem(this,Item);
		this._items[this._items.length-1].Initialize();
	};
	
	this.ImagesSrc = function()
	{
		var layer = document.getElementById(this._layerID);//wGet(this._layerID);
		var items = layer.getElementsByTagName("img"); 
		var numItems = items.length;
	   	for (var count = 1; count < numItems; ++count){
			items[count].src = this._imagesSrc[count-1];
		} 
	};
	
    this.Initialize = function()
    {
		var layer = document.getElementById(this._layerID);//wGet(this._layerID);
		this._info =  document.getElementById(this._infoID);
		this.AddInfoClickEvent(this._info);
		if (layer)
        {
			this._position._x = layer.offsetLeft;
			this._position._y = layer.offsetTop;
			this._size._x = layer.offsetWidth;
			this._size._y = layer.offsetHeight;
			if(this._imagesSrc != null) this.ImagesSrc();
			var items = layer.getElementsByTagName("A"); 
			var numItems = items.length;
			this._itemSize._x = items[0].offsetWidth;
			this._itemSize._y = items[0].offsetHeight;
			this._itemPosition._x = this._size._x / 2 - this._itemSize._x / 2;
			this._itemPosition._y = this._size._y / 2 - this._itemSize._y / 2;
	    	for (var count = 0; count < numItems; ++count){
				this.AddItem (items[count]);
				this.AddItemClickEvent(items[count]);
		    } 
        }
		
		//this._updateItems = (this._maxItems == 0)?this._items.length:this._maxItems;
		if(this._maxItems==0) this._maxItems = this._items.length;
		this._ini = true;
		this.Fly();
		this.Start();
    };

    this.Fly = function()
    {
		for(;this._unfreezedItems < this._maxItems;this._unfreezedItems++){
			this._lastFreezed < this._items.length-1? this._lastFreezed++:this._lastFreezed = 0;
			this._items[this._lastFreezed]._freeze = false;
		}
		if(this._updateItems >= this._items.length){
			this._updateItems = (this._maxItems == 0)?this._items.length:this._maxItems;
			        for (var count = 0; count < this._items.length; ++count)
					this._items[count]._freeze = false;
		}
        for (var count = 0; count < this._items.length; ++count)
		    this._items[count].Update();
	};

    this.Start = function()
    {
		if (this._interval == '')
            this._interval = setInterval(this._ID + '.Fly()', flyspeed);
    };

    this.Stop = function()
    {
        if (this._interval != '')
            clearInterval(this._interval);
        this._interval = '';
    };

    this.ClickItem = function(Index)
    {
		this._clickedItem = Index;
		this.ShowInfo();
		return false;/*true if you want to activate the link */
	};
	
    this.ShowInfo = function()
	{
	    	//runopacity("itemInfo", 100, 0, 1000);
		var infoTitle = document.getElementById("infoTitle");
		infoTitle.innerHTML = this._items[this._clickedItem].getTitleInnerHTML();
		var infoContent = document.getElementById("infoContent");
		infoContent.innerHTML = this._items[this._clickedItem].getBodyInnerHTML();
		runopacity("itemInfo", 0, 100, 1000);
    };

	this.AddItemClickEvent = function(Item){
		addEvent(Item,"click",function(e){This.DisplayInfo(Item);});
	}

	this.DisplayInfo = function(Item){
		//card.className = "largeCard";
		This.Stop();
		This._info.src = Item.getElementsByTagName('img')[0].src;
		This._info.style.display = "block";
	};

	this.AddInfoClickEvent = function(Item){
		addEvent(Item,"click",function(e){This.CloseInfo(Item);});
	}

	this.CloseInfo = function(info){
		info.style.display="none";
		This.Start();
	};	
 }
 
 
flyImages = new Array (
        "images/sears.png",
		"images/target.png",
		"images/macys.png",
		"images/jcpenney.png",
		"images/amazon.png",
		"images/shopnbc.jpg",
		"images/smarbargains.png",
		"images/AMERICAN_EXPRESS.jpg",
		"images/VISA.png",
		"images/MASTERCARD.png",
		"images/fedex.jpg",
		"images/upsworldship.jpg",
		"images/zebra.gif",
		"images/SaksFifthAvenuelogo.png",
		"images/walmart.jpg",
		"images/kmart.jpg",
		"images/datamax.jpg",
		"images/zales.jpg",
		"images/fredmeyer.jpg",
		"images/kohls.jpg",
		"images/aafes.jpg",
		"images/boscov.jpg",
		"images/fingerhut.bmp",
		"images/gxs.jpg",
		"images/hsn.jpg",
		"images/kassoy.jpg",
		"images/meijer.jpg",
		"images/mervyns.jpg",
		"images/mettler1.jpg",
		"images/mettler2.jpg",
		"images/nexcom2.jpg",
		"images/overstocks.jpg",
		"images/sartorius.png",
		"images/sato.jpg",
		"images/shopko.jpg",
		"images/spiegel.jpg",
		"images/spscommerce.jpg",
		"images/sterlingjewelers.jpg",
		"images/tiecommerce.jpg",
		"images/sterlingcommerce.gif"
);

var fly = new Fly('fly', 'fly',100,'flyInfo',flyImages);/*460, 64, 64);*/

//addEvent(window,"load",function(e){fly.Initialize();});


function addHoverDelay( obj, func, delay )    {
	if ( delay === undefined ) 	delay = 1000;  
	var hoverTimer;
	addEvent( obj, 'mouseover', function(e) {hoverTimer = setTimeout( func, delay );} );
	addEvent( obj, 'mouseout', function(e) {clearTimeout( hoverTimer );} );    
}

function callAddHoverDelay(obj,index){
	addHoverDelay(obj,function(){runAccordion(index);},10);
}

function accordionInit(){
	addEvent(document.getElementById("Accordion1"),"click",function(){runAccordion(1);},200);
	addEvent(document.getElementById("Accordion2"),"click",function(){runAccordion(2);},200);
	addEvent(document.getElementById("Accordion3"),"click",function(){runAccordion(3);},200);
	addEvent(document.getElementById("Accordion4"),"click",function(){runAccordion(4);},200);//slideSleep(endSlide,'fly.Initialize()',1000);},200);
	addEvent(document.getElementById("Accordion5"),"click",function(){runAccordion(5);},200);
	accordionTimer = setTimeout("runAccordion(openAccordionIndex==1?5:openAccordionIndex-1)",10000);
	//document.getElementById('Accordion' + ((openAccordionIndex==1)?5:openAccordionIndex-1)).click();",500);
	//addHoverDelay(document.getElementById("Accordion1"),function(){runAccordion(1);popCards.Init(cardsImages);},200);
	//addHoverDelay(document.getElementById("Accordion2"),function(){runAccordion(2);},200);
	//addHoverDelay(document.getElementById("Accordion3"),function(){runAccordion(3);popCards1.Init(cards1Images);},200);
	//addHoverDelay(document.getElementById("Accordion4"),function(){runAccordion(4);if(!fly._ini) slideSleep1();},200);//slideSleep(endSlide,'fly.Initialize()',1000);},200);
	//addHoverDelay(document.getElementById("Accordion5"),function(){runAccordion(5);},200);
}


addEvent(window,"load",function(e){accordionInit();});



var Contentwidth = 570;
var TimeToSlide = 250.0;
var endSlide = true;


var openAccordionIndex = '5';

function slideSleep(callfunc,func,sleep) {
	if(endSlide) {
		setTimeout('fly.Initialize()', 1);
    } else {
		setTimeout("slideSleep(endSlide,'fly.Initialize()',100)",200);
	}
}

function slideSleep1() {
	if(endSlide) {
		setTimeout('fly.Initialize()', 1);
    } else {
		setTimeout("slideSleep1()",200);
	}
}
var accordionTimer;
    

function runAccordion(index)
{
	clearTimeout(accordionTimer);
	openAccordion = "Accordion" + openAccordionIndex + "Content";
	endSlide = false;
	var nID = "Accordion" + index + "Content";
	//if(openAccordion == nID)
	//  nID = '';
	if(openAccordionIndex != index)   {
		document.getElementById("Accordion" + index).style.visibility = "hidden";
		document.getElementById("ContentFloat" + openAccordionIndex).style.display = "none";
		
		if (openAccordionIndex != '') document.getElementById("Accordion" + openAccordionIndex).style.visibility = "visible";
	
		setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ",'" + openAccordion + "','" + nID + "')", 33);

		openAccordionIndex = index;
	}
	else{
		endSlide = true;
	}
}

function animate(lastTick, timeLeft, closingId, openingId)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
  
  var opening = (openingId == '') ? null : document.getElementById(openingId);
  var closing = (closingId == '') ? null : document.getElementById(closingId);

	//alert("opening " + opening.offsetWidth + "width " + opening.style.width);
  
 
  if(timeLeft <= elapsedTicks)
  {
    if(opening != null) {
	        opening.style.width = Contentwidth + 'px';
			
			//pngfix(opening);
	}
    
    if(closing != null)
    {
      closing.style.display = 'none';
      closing.style.width = '0px';
	  //pngfix(closing);
    }
	endSlide = true;
	//alert("ContentFloat" + openAccordionIndex);
	document.getElementById("ContentFloat" + openAccordionIndex).style.display = "inline";
	//alert(endSlide);
	switch(openAccordionIndex){
		case 1:popCards.Init(cardsImages);
				break;
		case 3:popCards1.Init(cards1Images);
				break;
		case 4:if(!fly._ini) fly.Initialize();
			break;
	}
	accordionTimer = setTimeout("runAccordion(openAccordionIndex==1?5:openAccordionIndex-1)",10000);
    return;
  }
 
  timeLeft -= elapsedTicks;
  var newClosedwidth = Math.round((timeLeft/TimeToSlide) * Contentwidth);

  if(opening != null)
  {
    if(opening.style.display != 'inline-block')
      opening.style.display = 'inline-block';
    //Contentwidth = opening.offsetWidth;	
    opening.style.width = (Contentwidth - newClosedwidth) + 'px';
	
    //opening.offsetWidth = (Contentwidth - newClosedwidth) ;

  }
  
  if(closing != null){
    closing.style.width = newClosedwidth + 'px';
    //closing.offsetWidth = newClosedwidth ;
  }

  setTimeout("animate(" + curTick + "," + timeLeft +",'" + closingId + "','" + openingId + "')", 100);
}



//===============Cards
var numCards = 0;
var left =0;
var top = 0;
var spaceTop = 15;
var spaceLeft = 5;



function CardsInit(){
	
	var cards = document.getElementById("cards1");
	var items = cards.getElementsByTagName("img"); 
	numCards = items.length;
	//left = items[0].style.offsetLeft;
	//top = items[0].style.offsetTop;
	for (var count = 0; count < numCards; ++count){
		addSwitchEvent(items[count], count+1); // call a function and not addevent directly to avoid getting the same count value for all.
	}
	switchCards(1);
}

function addSwitchEvent(Item,Num){
	addEvent(Item,"click",function(e){switchCards(Num);});
}

function switchCards(topCard){
	var n = topCard;
	/*alert(topCard);*/
	for(var i=0; i < numCards; i++){
		var card = document.getElementById("card" + n);
		card.style.zIndex = 200 - i;
		card.style.left = left - i * spaceLeft + "px";
		card.style.top = top - i * spaceTop  +"px";
		n = (n == numCards) ? 1 : n+1;
	}
}


//addEvent(window,"load",function(e){CardsInit();});

 
function ListTick (id,speed){
	this._id = id;
	this._speed = speed;
	this._items = new Array();
	this._numItems = 0;
	this._interval = '';
	this._visibleItem = -1;
	
	var Speed = this._speed;
	var This = this;

	this.Tick = function(){
		if (this._visibleItem != -1 )
			this._items[this._visibleItem].style.display = "none";
		if(this._visibleItem < (this._numItems-1)) {
		       	this._visibleItem++;
		} else	{
			this._visibleItem=0;
		}
		this._items[this._visibleItem].style.display = "block";
	}

	this.Start = function()
    	{

        	if (this._interval == '')
            		this._interval = setInterval(this._id + ".Tick();", Speed);
	};

	this.Stop = function()
    	{
        	if (this._interval != ''){
            		clearInterval(this._interval);
			this._interval = '';
		}
    };
	
	function addMouseoverEvent(Item){
		addEvent(Item,"mouseover",function(e){This.DisplayInfo();});
	}
	function addMouseoutEvent(Item){
		addEvent(Item,"mouseout",function(e){This.CloseInfo();});
	}

	this.Init = function(){
		var list = document.getElementById(this._id);
		addMouseoverEvent(list);
		addMouseoutEvent(list);
		this._items = list.getElementsByTagName("li"); 
		this._numItems = this._items.length;
		for(var i=0;i<this._numItems;i++){
			this._items[i].style.display = "none";
		}
		this.Tick();
		this.Start();
	};
	
	this.CloseInfo = function(){
		//if (this._items[this._visibleItem].getElementsByTagName('span')[0]) this._items[this._visibleItem].getElementsByTagName('span')[0].style.display = "none";
		//this.Tick();
		this.Start();
	}
	
	this.DisplayInfo = function(){
		this.Stop();
		//if (this._items[this._visibleItem].getElementsByTagName('span')[0]) this._items[this._visibleItem].getElementsByTagName('span')[0].style.display = "block";
	}


}
var featuresTick = new ListTick("featuresTick",1000);
addEvent(window,"load",function(e){featuresTick.Init();});



function PopCards (id,speed,infoid){
	this._id = id;
	this._speed = speed;
	this._items = new Array();
	this._numItems = 0;
	this._interval = '';
	this._visibleItem = -1;
	this._infoID = infoid;
	this._info = null;
	
	var Speed = this._speed;
	var This = this;
	
	this.Pop = function(){
		if(this._visibleItem < (this._numItems-1)) {
		       	this._visibleItem++;
			this._items[this._visibleItem].style.visibility = "visible";
		} else	{
			this.Stop();
		}
	
	}

	this.Start = function()
    	{

        	if (this._interval == '')
            		this._interval = setInterval(this._id + ".Pop();", Speed);
	};

	this.Stop = function()
    	{
        	if (this._interval != ''){
            		clearInterval(this._interval);
			this._interval = '';
		}
    	};

	this.Init = function(images){
		var list = document.getElementById(this._id);
		this._items = list.getElementsByTagName("img"); 
		this._info =  document.getElementById(this._infoID);
		addMouseoutEvent(this._info);
		this._numItems = this._items.length;
		for(var i=1;i<this._numItems;i++){
			//this._items[i].style.display = "none";
			if(images != null){
				this._items[i].src=images[i];
			}
			addMouseoverEvent(this._items[i]); //don't call addevent directly to avoid unknowen item
			//addMouseoutEvent(this._items[i]);
		}
		this.Pop();
		this.Start();
	};

	function addMouseoverEvent(Item){
		addEvent(Item,"click",function(e){largeCard(Item);});
	}

	function largeCard(card){
		//card.className = "largeCard";
		This._info.src = card.src;
		This._info.style.display = "block";
		//This._info.style.visibility = "visible";
	
	};

	function addMouseoutEvent(Item){
		addEvent(Item,"click",function(e){smallCard(Item);});
	}

	function smallCard(card){
		//card.className = "card";
		This._info.style.display = "none";
	};

}
var cardsImages = new Array(
		"images/blank.png",
	    "images/email3z.jpg",
    	"images/email2z.jpg",
		"images/email1z.jpg",
		"images/email4z.jpg",
    	"images/email5z.jpg",
		"images/email6z.jpg",
		"images/email7z.jpg",
    	"images/email8z.jpg",
		"images/email9z.jpg"
);

var cards1Images = new Array(
		"images/blank.png",
		"images/crtnn.png",
		"images/crtn3z.jpg",
    	"images/crtn2z.jpg",
		"images/crtn4z.jpg",
		"images/crtn1z.jpg",
		"images/crtn6z.jpg",
		"images/crtn5z.jpg",
		"images/crtn9z.jpg",
		"images/crtn7z.jpg",
		"images/crtn8z.jpg"
);
var popCards = new PopCards("popCards2",350,"popCardsInfo");
var popCards1 = new PopCards("popCards1",350,"popCards1Info1");
//addEvent(window,"load",function(e){popCards.Init();});
//addEvent(window,"load",function(e){runAccordion(5);});

