/*
var SliderCarousel1 = new Object();

SliderCarousel1 = function(thumbDivId,slideDivId,orientation) {
	this.thumbDivId   = thumbDivId;
	this.slideDivId   = slideDivId;
	this.orientation  = orientation;
};
*/

//SliderCarousel1.prototype = {
//var SliderCarousel1 = {

/*	
thumbDivId     : div id name of thumbslider
slideDivId     : div id name of slide
orientation    : 'h'=horizontal or 'v'=verticle (optional)
slideType      : 1=quick move, 2=speed control (optional)
slideAutoDelay : Auto scroll. # of seconds before next slide. 0=no auto move (optional)
firstSlide     : # for first slide. (optional)
*/

function SliderCarousel1(thumbDivId,slideDivId,orientation,slideType,slideAutoDelay,firstSlide) {
	
	if (typeof(thumbDivId)=='undefined') {
		alert('thumbDivId is required in SliderCarousel1.');
		this.thumbDivId   = '';
	} else {
		this.thumbDivId   = thumbDivId;
	}

	if (typeof(slideDivId)=='undefined') {
		alert('slideDivId is required in SliderCarousel1.');
		this.slideDivId   = '';
	} else {
		this.slideDivId   = slideDivId;
	}

	if (typeof(orientation)=='undefined') orientation = 'h';
	this.orientation = orientation;

	if (typeof(slideType)=='undefined') slideType = 2;
	this.slideType    = slideType;

	if (typeof(slideAutoDelay)=='undefined') slideAutoDelay = 0;
	this.slideAutoDelay    = slideAutoDelay;

	if (typeof(firstSlide)=='undefined') {
		this.slideCurId      = 1;
	} else {
		this.slideCurId      = parseInt(firstSlide,10);
	}

	this.slideDelay      = 4;
	this.slideDataCnt    = 0;
	this.slideLastId	 = 0;
	this.slideIntv       = "";
	this.slideActive     = false;
	this.slideAutoIntv   = "";
	this.slideAutoActive = false;
	this.slideData       = new Object();

	this.change      	 = "";

	this.s_top    = 0;
	this.s_left   = 0;
	this.m_width  = 0;
	this.m_height = 0;
	this.l_width  = 0;
	this.l_height = 0;
	this.s_width  = 0;
	this.s_height = 0;
	
	this.t_width  = 0;
	this.t_height = 0;
	this.s_hor = 0;
	this.s_ver = 0;
	this.s_rhor = 0;
	this.s_rver = 0;
	this.p_top  = 0;
	this.p_left = 0;
	this.s_step = 20;

	this.cnt = 0;

	// Load data object
	this.addItem = function (title,image_small,image_large,url,target) {
		var data = new Object();
		data.title = title;
		data.image_small = image_small;
		data.image_large = image_large;
		data.url = url;
		data.target = target;
		this.slideDataCnt++;
		this.slideData[this.slideDataCnt] = data;
		// preload
		jQuery("<img>").attr("src", image_small);
		jQuery("<img>").attr("src", image_large);
	};

	this.init = function() {    

		if(typeof(SliderCarousel1_Object)=="undefined") SliderCarousel1_Object = new Object();
		SliderCarousel1_Object[this.thumbDivId] = this;

		// Init
		var w = $("#" + this.thumbDivId).width()+100;
		var h = $("#" + this.thumbDivId).height()+100;

		$("#" + this.thumbDivId).append("<ul style='width:"+(w*this.slideDataCnt)+"px;height:"+(h*this.slideDataCnt)+"px;position:relative;list-style:none;padding:0;margin:0;top:0;left:0;'></ul>");
		$("#" + this.thumbDivId).hide();
		
		this.slideLastId = this.slideCurId;

		// Draw thumbs
		for(var i=1; i<this.slideDataCnt+1; i++) {
			var c = "out";
			if (i==this.slideCurId) c = "on";
			$("#"+this.thumbDivId+" ul").append("<li rel='"+i+"_"+this.thumbDivId+"' class='"+c+"'><img src='"+this.slideData[i].image_small+"' alt='Thumb "+i+"'></li>");
		}
		$("#" + this.thumbDivId).fadeIn('slow');

		// Draw slides
		$("#" + this.slideDivId).append("<div class='slide_divB' id='"+this.slideDivId+"_divB'><img id='"+this.slideDivId+"_imageB' src='' alt=''></div>");
		$("#" + this.slideDivId).append("<div class='slide_divA' id='"+this.slideDivId+"_divA'><img id='"+this.slideDivId+"_imageA' src='' alt=''></div>");

		// Calculate thumb values
		this.s_top    = $("#"+this.thumbDivId).offset().top;
		this.s_left   = $("#"+this.thumbDivId).offset().left;
		this.m_width  = $("#"+this.thumbDivId).width();
		this.m_height = $("#"+this.thumbDivId).height();
		this.l_width  = $("#"+this.thumbDivId+" ul li").width();
		this.l_height = $("#"+this.thumbDivId+" ul li").height();
		this.s_width  = parseInt((this.l_width * $("#"+this.thumbDivId+" ul li").length ) - this.m_width);
		this.s_height = parseInt((this.l_height * $("#"+this.thumbDivId+" ul li").length ) - this.m_height);

		this.t_width  = this.m_width/3;
		this.t_height = this.m_height/3;

		// Attach event handlers
		$("#"+this.thumbDivId+" ul li").click(this.thumbClick);
		$("#"+this.thumbDivId+" ul li").mouseover(this.thumbOver);
		$("#"+this.thumbDivId+" ul li").mouseout(this.thumbOut);
		$("#"+this.thumbDivId).mousemove(this.thumbMove);
		$("#"+this.thumbDivId).mouseout(this.thumbAllOut);
	
		$("#"+this.slideDivId+"_divA").click(this.slideClick);  
	
		if (this.slideAutoDelay>0) {
			this.slideAutoActive = true;
			this.slideScroll(this.slideCurId);
		} else {
			this.thumbChange(this.slideCurId,this.thumbDivId);
		}

	};

	// Thumb click
	this.thumbClick = function (e) {

		var rel = $(this).attr('rel');
		var id = parseInt(rel.substr(0,rel.indexOf("_")),10);
		var name = rel.substr(rel.indexOf("_")+1);
		var obj = SliderCarousel1_Object[name];
		obj.slideCurId = id;

		obj.slideAutoActive = false;
		if (obj.slideAutoDelay>0) {
			// Restart motion after 10 sec.
			clearTimeout(obj.slideAutoIntv);
			obj.slideAutoIntv = setTimeout("SliderCarousel1_Object['"+obj.thumbDivId+"'].slideScroll(-1)",1000*10);
		}

		// Stop all motion
		clearTimeout(obj.slideIntv);
		obj.slideActive = false;
		obj.s_ver = 0;
		obj.s_hor = 0;
		
		obj.thumbChange(id,name);

	};

	// Thumb change
	this.thumbChange = function (i,name) {

		var obj = SliderCarousel1_Object[name];
		obj.slideCurId = i;

		if (obj.slideAutoActive) {

			if (obj.orientation=="v") {
			
				var pt = ((obj.l_height*obj.slideCurId)-(obj.l_height/2))
				var mt = (obj.m_height/2);
				var st = (obj.l_height*obj.slideDataCnt) - mt;
				var np_top = 0;		
				if (pt>mt) {
					if (pt>st) {
						np_top = -(st-mt);
					} else {
						np_top = -(pt-mt);
					}
				}
				np_top = Math.round(np_top);
				
				if (obj.p_top != np_top) {
					obj.moveThumb(np_top);
				} else {
					obj.p_top = np_top;
				}
			}
			if (obj.orientation=="h") {

				var pt = ((obj.l_width*obj.slideCurId)-(obj.l_width/2))
				var mt = (obj.m_width/2);
				var st = (obj.l_width*obj.slideDataCnt) - mt;
				var np_left = 0;		
				if (pt>mt) {
					if (pt>st) {
						np_left = -(st-mt);
					} else {
						np_left = -(pt-mt);
					}
				}
				np_left = Math.round(np_left);
				
				if (obj.p_left != np_left) {
					obj.moveThumb(np_left);
				} else {
					obj.p_left = np_left;
				}
			}

		}
		
		var t = obj.slideData[obj.slideCurId].title.replace("'","\'");

		// Flip slide
		$("#"+obj.slideDivId+"_imageB").attr('src', $("#"+obj.slideDivId+"_imageA").attr('src'));
		$("#"+obj.slideDivId+"_divA").hide();
		$("#"+obj.slideDivId+"_imageA").attr('src', obj.slideData[obj.slideCurId].image_large);
		$("#"+obj.slideDivId+"_imageA").attr('alt', t);
		$("#"+obj.slideDivId+"_imageA").attr('title', t);
		$("#"+obj.slideDivId+"_divA").fadeIn('slow');

		$("#"+obj.thumbDivId+" ul li[rel='"+obj.slideLastId+"_"+obj.thumbDivId+"']").attr('class', '');
		$("#"+obj.thumbDivId+" ul li[rel='"+obj.slideCurId+"_"+obj.thumbDivId+"']").attr('class', 'on');

		obj.slideLastId = obj.slideCurId;  

		if (obj.slideData[obj.slideCurId].url!="") {
			$("#"+obj.slideDivId+"_imageA").css('cursor','pointer');
		} else {
			$("#"+obj.slideDivId+"_imageA").css('cursor','default');
		}

		if(typeof(obj.change)=="function") obj.change(obj.slideCurId);
	};
	
	// Thumb over
	this.thumbOver = function () {
		if ($(this).attr('class')!='on') $(this).attr('class', 'over');
	};

	// Thumb over
	this.thumbAllOut = function () {
	};

	// Thumb out
	this.thumbOut = function () {
		if ($(this).attr('class')!='on') $(this).attr('class', 'out');
	};

	// Thumb move
	this.thumbMove = function (e) {

		var obj = SliderCarousel1_Object[this.id];

		obj.slideAutoActive = false;

		if ( (obj.orientation=="h" && obj.s_width>0) || (obj.orientation=="v" && obj.s_height>0) ) {

			if (obj.slideType==1) {
				if (obj.orientation=="h") {
					var curX = e.pageX - obj.s_left - (obj.l_width  / 2);
					if (curX < 0) curX = 0;
					if (curX > obj.m_width  - obj.l_width ) curX = obj.m_width  - obj.l_width;
					obj.p_left = -Math.round((curX * 100 / (obj.m_width  - obj.l_width )) * obj.s_width  / 100);
					if (obj.p_left!='NaN') $("#"+obj.thumbDivId+" ul").css('left',obj.p_left);
				}
				if (obj.orientation=="v") {
					var curY = e.pageY - obj.s_top  - (obj.l_height / 2);
					if (curY < 0) curY = 0;
					if (curY > obj.m_height - obj.l_height) curY = obj.m_height - obj.l_height;
					obj.p_top  = -Math.round((curY * 100 / (obj.m_height - obj.l_height)) * obj.s_height / 100);
					if (obj.p_top!='NaN')  $("#"+obj.thumbDivId+" ul").css('top',obj.p_top);
				}
			}

			if (obj.slideType==2) {
				if (obj.orientation=="h") {
					var curX = e.pageX - obj.s_left;
					if (curX<=obj.t_width) {
						obj.s_rhor = obj.s_step - (curX * obj.s_step / obj.t_width);
					}
					if (curX>obj.t_width && curX<(obj.t_width*2)) {
						obj.s_rhor = 0;
					}
					if (curX>=(obj.t_width*2)) {
						obj.s_rhor = (((obj.t_width*3)-curX) * obj.s_step / obj.t_width) - obj.s_step;
					}
				}
				if (obj.orientation=="v") {
					var curY = e.pageY - obj.s_top;
					if (curY<=obj.t_height) {
						obj.s_rver = obj.s_step - (curY * obj.s_step / obj.t_height);
					}
					if (curY>obj.t_height && curY<(obj.t_height*2)) {
						obj.s_rver = 0;
					}
					if (curY>=(obj.t_height*2)) {
						obj.s_rver = (((obj.t_height*3)-curY) * obj.s_step / obj.t_height) - obj.s_step;
					}
				}
			}
			
			if (obj.slideType==2 && (obj.s_rver!=0 || obj.s_rhor!=0)) {
				if (obj.slideActive==false) obj.slideSpeed();
			}

		}
	};

	// Slide click
	this.slideClick = function () {
		var obj = slider1;
		if (obj.slideData[obj.slideCurId].url!="") {
			location.href = obj.slideData[obj.slideCurId].url;
		}
	};

	// Slide scroll
	this.moveThumb = function (p) {
		if (this.orientation=="v") {
			if (p!=this.p_top) {
				if (p<this.p_top) {
					this.p_top = this.p_top - (this.s_step/1);
					if (this.p_top<p) this.p_top = p;
				}
				if (p>this.p_top) {
					this.p_top = this.p_top + (this.s_step/1);
					if (this.p_top>p) this.p_top = p;
				}
				$("#"+this.thumbDivId+" ul").css('top',this.p_top);
				setTimeout("SliderCarousel1_Object['"+this.thumbDivId+"'].moveThumb("+p+")",50);
			}
		}
		if (this.orientation=="h") {
			if (p!=this.p_left) {
				if (p<this.p_left) {
					this.p_left = this.p_left - (this.s_step/1);
					if (this.p_left<p) this.p_left = p;
				}
				if (p>this.p_left) {
					this.p_left = this.p_left + (this.s_step/1);
					if (this.p_left>p) this.p_left = p;
				}
				$("#"+this.thumbDivId+" ul").css('left',this.p_left);
				setTimeout("SliderCarousel1_Object['"+this.thumbDivId+"'].moveThumb("+p+")",50);
			}
		}
	};
	
	// Slide scroll
	this.slideScroll = function (i) {
		if (i==-1) {
			this.slideAutoActive = true;
			i = 0;
		}
		if (this.slideAutoActive) {
			if (i==0) {
				i = this.slideCurId + 1;
				if (i > this.slideDataCnt) i = 1;
			}
			this.thumbChange(i,this.thumbDivId);
			this.slideAutoIntv = setTimeout("SliderCarousel1_Object['"+this.thumbDivId+"'].slideScroll(0)",1000*this.slideAutoDelay);
		}
	};

	// Slide speed scroll
	this.slideSpeed = function () {

		this.cnt++;
		if (this.orientation=="h") {

			if (this.s_rhor < this.s_hor) {
				this.s_hor = this.s_hor - (this.s_step/1);
				if (this.s_rhor > this.s_hor) this.s_hor = this.s_rhor;
			}
			if (this.s_rhor > this.s_hor) {
				this.s_hor = this.s_hor + (this.s_step/1);
				if (this.s_rhor < this.s_hor) this.s_hor = this.s_rhor;
			}
			if (this.s_rhor<0) { // slides move up, mouse lower area
				this.p_left+=this.s_hor;
				if (-this.p_left>this.s_width) {
					this.p_left = -this.s_width;
					this.s_hor = 0;
				}
			}
			if (this.s_rhor>0) { // slides move down, mouse upper area
				this.p_left+=this.s_hor;
				if (this.p_left>0) {
					this.p_left = 0;
					this.s_hor = 0;
				}
			}
			$("#"+this.thumbDivId+" ul").css('left',this.p_left);
		}
		if (this.orientation=="v") {
			if (this.s_rver < this.s_ver) {
				this.s_ver = this.s_ver - (this.s_step/10);
				if (this.s_rver > this.s_ver) this.s_ver = this.s_rver;
			}
			if (this.s_rver > this.s_ver) {
				this.s_ver = this.s_ver + (this.s_step/10);
				if (this.s_rver < this.s_ver) this.s_ver = this.s_rver;
			}
			if (this.s_rver<0) { // slides move up, mouse lower area
				this.p_top += this.s_ver;
				if (-this.p_top > this.s_height) {
					this.p_top = -this.s_height;
					this.s_ver = 0;
				}
			}
			if (this.s_rver>0) { // slides move down, mouse upper area
				this.p_top += this.s_ver;
				if (this.p_top > 0) {
					this.p_top = 0;
					this.s_ver = 0;
				}
			}
			$("#"+this.thumbDivId+" ul").css('top',this.p_top);
		}

		if (this.s_ver!=0 || this.s_hor!=0) {
			this.slideActive = true;
			this.slideIntv = setTimeout("SliderCarousel1_Object['"+this.thumbDivId+"'].slideSpeed()", 50);
		} else {
			clearTimeout(this.slideIntv);
			this.slideActive = false;
		}
	};

};

