

var fader;
var Numberlinks;
var IdLinks;
var fr;
var catalog;
/*****************************************************************************
 * Fade Rotator 
 *****************************************************************************/
function FadeRotator(foreground, background, jpgs, fadetime, delay, randomize ) {
	this.fgId = foreground;
	this.bgId = background;
	this.images = jpgs;
	this.fadetime = fadetime;
	this.delay = delay;
	this.inOut = this.IN;
	this.next = 0;
	this.current = 0;
	this.intervalId = 0;
	this.opacity = 0;
	this.randomize = randomize;
}	

FadeRotator.prototype.IN = 0;
FadeRotator.prototype.OUT = 0;
FadeRotator.setOpacity = setOpacity;

FadeRotator.prototype.fadeImage = function() {
	var image = document.getElementById(this.fgId);
	FadeRotator.setOpacity(image,this.opacity);
	image.style.visibility = 'visible';
	var p = this;
	window.setTimeout(function() { p.fadeInOut() }, this.delay);
}

FadeRotator.prototype.setOpValAndContinue = function() {
	var obj = document.getElementById(this.fgId);
	if (this.inOut == this.IN) { 
		if (this.opacity <= 100) {
			FadeRotator.setOpacity(obj,this.opacity);
			this.opacity += 3;
			return true;
		}
	}
	else {
		if (this.opacity >= 0) {
			FadeRotator.setOpacity(obj,this.opacity);
			this.opacity -= 3;
			return true;
		}
	}
	return false;
}

FadeRotator.prototype.fadeInOut = function(val) {
	var p=this;
	if (this.intervalId == 0) {
		this.intervalId = window.setInterval(function() { p.fadeInOut() }, this.fadetime);
	}
	if (!this.setOpValAndContinue()) {
		window.clearInterval(this.intervalId);
		this.intervalId = 0;
		this.inOut = this.inOut ^ 1;
		if (val != 1) {
			this.current = this.next;
			this.next = this.getNextImageIndex(true); 
		}
		this.setImage(this.inOut);
		window.setTimeout(function() { p.fadeInOut() }, this.delay);
	}
}
FadeRotator.prototype.setImage = function(inOutType) {
	
	if (inOutType == this.IN) {
		document.getElementById(this.fgId).src = "images/" + this.images[this.next];
	}
	else {
		document.getElementById(this.bgId).style.backgroundImage = 'url(images/' + this.images[this.next] + ')';
	}
	this.setViewLink();
	
}

FadeRotator.prototype.getNextImageIndex = function (type) {
	if (this.randomize) {
		return getRandomNum(this.images.length);
	}
	else {
		return (++this.next % (this.images.length));
	}
}


FadeRotator.prototype.setViewLink = function () {
	if (document.getElementById("view_link")) {
		document.getElementById("view_link").href = "Bookmark.aspx?BookmarkNumber=" + NumberLinks[fr.current];
	}
}
/*****************************************************************************
 * Fader
 *****************************************************************************/
function Fader(div, backDiv, interval) {
	this.interval = interval;
	this.objId = div;
	this.backId = backDiv;
	this.intervalId = 0;
	this.opacity = 0;
	image = document.getElementById(this.objId);
	image.style.visibility = 'visible';
}

Fader.setOpacity = setOpacity;
Fader.setFrontBackLink = setFrontBackLink;

Fader.prototype.fadeIn = function() {
	if (document.getElementById) {
		obj = document.getElementById(this.objId);
		var aId = document.getElementById("frontBack");
		if (this.intervalId == 0) {
			this.opacity = 0;
			Fader.setOpacity(obj, this.opacity);
			var p = this;
			this.intervalId = window.setInterval(function() {p.fadeIn()}, this.interval);
		}
		
		if (this.opacity <= 100) {
			Fader.setOpacity(obj, this.opacity);
			this.opacity += 3;
		}
		else {
			window.clearInterval(this.intervalId);
			this.intervalId = 0;
			aId.href = "javascript:fader.fadeOut()";
			Fader.setFrontBackLink(true);
			//aId.innerHTML = "back view";
		}
	}	
}
Fader.prototype.fadeOut = function() {
	if (document.getElementById) {
		obj = document.getElementById(this.objId);
		var aId = document.getElementById("frontBack");
		if (this.intervalId == 0) {
			this.opacity = 100;
			var p = this;
			Fader.setOpacity(obj, this.opacity);
			this.intervalId = window.setInterval(function() {p.fadeOut()}, this.interval);
		}
		
		if (this.opacity >= 0) {
			Fader.setOpacity(obj, this.opacity);
			this.opacity -= 3;
		}
		else {
			window.clearInterval(this.intervalId);
			this.intervalId = 0;
			aId.href = "javascript:fader.fadeIn()";
			//aId.innerHTML = "front view";
			Fader.setFrontBackLink(false);
		}
	}	
}
/*****************************************************************************
 * Functions 
 *****************************************************************************/
function setOpacity(obj,op) {
	op = (op == 100)?99.999:op;
	if (obj) {
		obj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity="+op+")";
		//obj.style.filter = "alpha(opacity:"+op+")";			  
		obj.style.KHTMLOpacity = op/100;			  
		obj.style.MozOpacity = op/100;
		obj.style.opacity = op/100;
	}
	else {
		alert("object in setOpacity is null");
	}
}
function getRandomNum(len) {
	return Math.floor(Math.random()*len);
}

function homeOnLoad() {
    NumberLinks = new Array(    
				"0710", "0005", "0948", 
				"0013", "0401", "0705",
				"0640", "0518", "0821", 
				"0280", "0960", "0771", 
				"0980",
				"0489", "0132", "0091" 
			   );
    var theImages = new Array(  
				"Featured1a.jpg", "Featured2a.jpg", "Featured3a.jpg", 
				"Featured11.jpg", "Featured12.jpg", "Featured13.jpg", 
				"Featured7a.jpg", "Featured8a.jpg", "Featured9a.jpg", 
				"Featured10.jpg", "Featured4a.jpg", "Featured5a.jpg", 
				"Featured14.jpg",
				"Featured6a.jpg", "Featured7.jpg",  "Featured8.jpg"
			     );
    var picture = document.getElementById("picture");
    picture.style.backgroundImage = "url(images/Featured2a.jpg)";
    picture.style.backgroundColor = "#18183d";
    picture.style.backgroundRepeat = "no-repeat";
    picture.style.bacgroundPosition = "left top";
    fr = new FadeRotator('featurePic','picture',theImages, 25, 4000);
	document.getElementById("featurePic").style.visibility = "visible";	    
	fr.fadeImage();
}

function detailOnLoad(photo1,photo2) {
    //fader = new Fader(photo1,photo2, 25);
    catalog = false;
    var back = document.getElementById(photo2).firstChild;
    if (back.src.search(/generic/i) > -1) {
        document.getElementById(photo2).style.visibility = "hidden";
        setOpacity(document.getElementById(photo2),0);
        document.getElementById("frontBack").style.visibility = "hidden"; 
    }
    else {
        if (back.src.search(/highligh/i) > -1){
            catalog = true;
        }
        setFrontBackLink(true);
        document.getElementById(photo2).style.visibility = "visible";
        setOpacity(document.getElementById(photo2),100);
        document.getElementById("frontBack").style.visibility = "visible";
    }
    fader = new Fader(photo1,photo2, 25);
}
function setFrontBackLink(front) {
    var aId = document.getElementById("frontBack");
    var label = "";
    if (front) {
        if (catalog) {
            label = "highlight bookmarks";
        }
        else {
            label = "View Back";
        }
    }
    else {
        if (catalog) {
            label = "unhighlight bookmarks";
        }
        else{
            label = "View Front";
        }
    }
    aId.innerHTML = label;
}
function myhomeOnLoad() {
    var picture = document.getElementById("header_pic");
    picture.style.backgroundImage = "url(images/header1.jpg)";
    picture.style.backgroundRepeat = "no-repeat";
	picture.style.backgroundPosition = "left top";
    var fr = new FadeRotator('img_pic','header_pic',new Array("header1.jpg", "header2.jpg", "header3.jpg", 
															  "header4.jpg","header5.jpg","header6.jpg",
															  "header7.jpg","header8.jpg","header9.jpg",
															  "header10.jpg","header11.jpg","header12.jpg",
															  "header13.jpg","header14.jpg","header15.jpg",
															  "header16.jpg","header17.jpg","header18.jpg",
															  "header19.jpg","header20.jpg","header21.jpg",
															  "header22.jpg","header23.jpg","header24.jpg",
															  "header25.jpg","header26.jpg","header27.jpg",
															  "header100.jpg","header101.jpg","header102.jpg",
															  "header103.jpg","header104.jpg","header105.jpg",
															  "header106.jpg","header107.jpg","header108.jpg",
															  "header109.jpg"), 
															  25, 5000, false);
	document.getElementById("img_pic").style.visibility = "visible";	    
	fr.fadeImage();
}
var setGradient = (function(){
	 
	//private variables;
	var p_dCanvas = document.createElement('canvas');
	var p_useCanvas =  !!( typeof(p_dCanvas.getContext) == 'function');
	var p_dCtx = p_useCanvas?p_dCanvas.getContext('2d'):null;
	var p_isIE = /*@cc_on!@*/false;
	 
	
	 //test if toDataURL() is supported by Canvas since Safari may not support it
	
   try{   p_dCtx.canvas.toDataURL() }catch(err){
          p_useCanvas = false ;
   };
         
	if(p_useCanvas){
	   
	   return function (dEl , sColor1 , sColor2 , bRepeatY ){
			
			if(typeof(dEl) == 'string') dEl =  document.getElementById(dEl);
			if(!dEl) return false;
			var nW = dEl.offsetWidth;
			var nH = dEl.offsetHeight;
			p_dCanvas.width = nW;
			p_dCanvas.height = nH;
			
		
			var dGradient;
			var sRepeat;
			// Create gradients
			if(bRepeatY){
				dGradient = p_dCtx.createLinearGradient(0,0,nW,0);
				sRepeat = 'repeat-y';
			}else{
				dGradient = p_dCtx.createLinearGradient(0,0,0,nH);
				sRepeat = 'repeat-x';
			}		
			
			dGradient.addColorStop(0,sColor1);
			dGradient.addColorStop(1,sColor2);				
			
			p_dCtx.fillStyle = dGradient ; 
			p_dCtx.fillRect(0,0,nW,nH);
			var sDataUrl = p_dCtx.canvas.toDataURL('image/png');
			
			with(dEl.style){
				backgroundRepeat = sRepeat;
				backgroundImage = 'url(' + sDataUrl + ')';
				backgroundColor = sColor2;    
			};
	   }
	}else if(p_isIE){
		
		p_dCanvas = p_useCanvas = p_dCtx =  null;		
		return function (dEl , sColor1 , sColor2 , bRepeatY){
			if(typeof(dEl) == 'string') dEl =  document.getElementById(dEl);
			if(!dEl) return false;
			dEl.style.zoom = 1;
			var sF = dEl.currentStyle.filter;
			dEl.style.filter += ' ' + ['progid:DXImageTransform.Microsoft.gradient(	GradientType=',  +(!!bRepeatY ),',enabled=true,startColorstr=',sColor1,', endColorstr=',sColor2,')'].join('');
		    
		};
	
	}else{
		
		p_dCanvas = p_useCanvas = p_dCtx =  null;
		return function(dEl , sColor1 , sColor2  ){
			
			if(typeof(dEl) == 'string') dEl =  document.getElementById(dEl);
			if(!dEl) return false;
			with(dEl.style){
				 backgroundColor = sColor2; 
			};
			//alert('your browser does not support gradient effet');
		}
	}
})();

