var IE6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6")!=-1)) ? 1 : 0;
var IE7 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 7")!=-1)) ? 1 : 0;
var Opera = ((navigator.appName.indexOf("Opera")!=-1)) ? 1 : 0;


var defaultConfig = {
	/**  
	 * The delay in seconds before sliding up
	 */
	upDelay: 4, 
	
	/** 
	 * The delay in seconds before sliding down 
	 */
	downDelay: 10, 
	
	/**
	 *  The behaviour of the slider 
	 */
	behaviour: "up", 
	
	behaviourType: { /* The possible behaviours of the slider */
		up: 0,
		upDown: 1,
		looping: 2
	},
	
	skin: "fb",
	size : "normal",
	skinType: {
		fb : {
			normal : {width : '200', height : '197', down_pos : '-202'},
			big : {width : '240', height : '209', down_pos : '-214'}
		},
		tv : {
			normal : {width : '284', height : '234', down_pos : '-239'},
	 		big : {width : '314', height : '254', down_pos : '-259'}
		}
	},
	
	/**
	 * This property is only for the TV skin.
	 */
	color: "green",
	colorType: {
		green: 0,
		red: 1,
		blue: 2
	},
	
	layoverIndex: 0, /* The index of the wished layover text */
	maxLayoverIndex: 5, /* The highest index */
	targetOnPrivate: "chatroom", /* The target page in case the model is on private */
	targetType: { /* The possible target pages */
		chatroom: 0,
		allonline: 1
	},
	category: "girl", /* The category you can choose from */
	psid:   'awempire', //@TODO ilyenbol meg lehet, hogy lesz. Ezt ti tudjatok.
	pstool: '28_337',
	pstour: 't1',
	psprogram: 'REVS',
	campaign_id: '-1',
	template_id: "337",
	
	iframeUrl: "http://rfc.awempire.com/?tid=test_new",
	urlParameters: [
		"skin",
		"size",
		"color",
		"flags",
		"psid",
		"layoverIndex",
		"targetOnPrivate",
		"pstool",
		"pstour",
		"psprogram",
		"campaign_id",
		"site",
		"cobrand_site_id"
	],

	setOptions: function(obj) {
		if (typeof(obj) == 'object') {
			for (var x in obj) {
				this[x] = obj[x];
			}
		}
	}
}

/**
 * The Slider Class
 * @type 
 */
var Slider = {
	
	config: defaultConfig,
	
	containerId: 'AWE_Slider_Container',
	
	divId: 'AWE_Slider',
	
	delay: 0,
	
	css: 'position:fixed;',
	
	isInited: false,
	
	slides: 0,
	
	init: function(conf){
		

		
		if (conf)
		{
			if (conf.upDelay && typeof(conf.upDelay) == "number")
			{
				this.config.upDelay = parseInt(conf.upDelay);
			}
			
			if (conf.downDelay && typeof(conf.downDelay) == "number")
			{
				this.config.downDelay = parseInt(conf.downDelay);
			}
			
			if (conf.hideDelay && typeof(conf.hideDelay) == "number")
			{
				this.config.hideDelay = parseInt(conf.hideDelay);
			}
			
			if (conf.behaviour && typeof(this.config.behaviourType[conf.behaviour]) == "number")
			{
				this.config.behaviour = conf.behaviour;
			}
			
			if (conf.layoverIndex && typeof(conf.layoverIndex) == "number" && conf.layoverIndex >= 0 && conf.layoverIndex <= this.config.maxLayoverIndex)
			{
				this.config.layoverIndex = parseInt(conf.layoverIndex);
			}
			
			if (conf.targetOnPrivate && typeof(this.config.targetType[conf.targetOnPrivate]) == "number")
			{
				this.config.targetOnPrivate = conf.targetOnPrivate;
			}
			
			if (conf.category && typeof(this.config.categoryType[conf.category]) == "number")
			{
				this.config.category = conf.category;
			}
			
			if (conf.skin && typeof(this.config.skinType[conf.skin]) == "object")
			{
				this.config.skin = conf.skin;
			}
			
			if (conf.size && typeof(this.config.skinType[this.config.skin][conf.size]) == "object")
			{
				this.config.size = conf.size;
			}
			
			if (conf.color && typeof(this.config.colorType[conf.color]) == "number")
			{
				this.config.color = conf.color;
			}
		}
		
		this.banSizeObj = this.config.skinType[this.config.skin][this.config.size];
		
		this.configToUrl();
		
		this.getContainer().appendChild(this.getInnerDiv());
		document.body.appendChild(this.getContainer());
		
		this.isInited = true;
		
		this.manageSlide();
	},
	
	getContainer: function(){
		if (!this.container || !(this.container.nodeType === 1)){
			this.container = document.createElement("div");
			this.container.setAttribute("id", this.containerId);
			this.container.style.cssText = this.css + "position:absolute; display:none; z-index:99999; width:" + this.banSizeObj.width + "px; height:" + this.banSizeObj.height +"px; overflow:hidden; background-color:transparent; border:0px; right: 0px; top:0px";
			var contId = this.containerId;
			function positionSlider(){
				$("#"+contId).css({position: "absolute", top:($(window).scrollTop()+$(window).height()-$("#"+contId).height())+"px"});
			}

			setTimeout("moveSlider()", 100);
			$(document).ready(function(){
				$(window).onload = positionSlider();
				$(window).scroll(positionSlider).resize(positionSlider);
			});
		}
		
		return this.container;
	},
	
	getInnerDiv: function(){
		if (!this.innerDiv || !(this.innerDiv.nodeType === 1)) {

			this.innerDiv = document.createElement("div");
			this.innerDiv.setAttribute("id", this.divId);
			this.innerDiv.style.cssText = "border:none; overflow: hidden; position:relative; top:" + this.banSizeObj.height +"px; z-index:99998; width:" + this.banSizeObj.width + "px; height:" + this.banSizeObj.height + "px; ";
			var span  = document.createElement("span");
			switch (defaultConfig.template_id){
				
				case "337":
					$(span).addClass("closebtn").css({
						'position':'absolute',
						'cursor':'pointer',
						'left':'217px',
						'top':'4px',
						'font-family':'Comic Sans MS',
						'font-size':'11px',
						'font-weight':'bold',
						'color':'DarkGray'
					});

					if (IE6) {
						$(span).addClass("closebtn").css({
							'position':'absolute',
							'cursor':'pointer',
							'left':'228px',
							'top':'5px',
							'font-family':'Comic Sans MS',
							'font-size':'11px',
							'font-weight':'bold',
							'color':'DarkGray'
						});
					}if  (IE7) {
						$(span).addClass("closebtn").css({
							'position':'absolute',
							'cursor':'pointer',
							'left':'230px',
							'top':'3px',
							'font-family':'Comic Sans MS',
							'font-size':'11px',
							'font-weight':'bold',
							'color':'DarkGray'
						});
					}
					break;
					
					
					case "338":
					$(span).addClass("closebtn").css({
						'position':'absolute',
						'cursor':'pointer',
						'left':'177px',
						'top':'4px',
						'font-family':'Comic Sans MS',
						'font-size':'11px',
						'font-weight':'bold',
						'color':'DarkGray'
					});
					if (IE6) {
						$(span).addClass("closebtn").css({
							'position':'absolute',
							'cursor':'pointer',
							'left':'185px',
							'top':'3px',
							'font-family':'Comic Sans MS',
							'font-size':'11px',
							'font-weight':'bold',
							'color':'DarkGray'
						});
					}if  (IE7) {
					$(span).addClass("closebtn").css({
						'position':'absolute',
						'cursor':'pointer',
						'left':'185px',
						'top':'3px',
						'font-family':'Comic Sans MS',
						'font-size':'11px',
						'font-weight':'bold',
						'color':'DarkGray'
					});
					}
					break;
					
					
					case "339":
					$(span).addClass("closebtn").css({
						'position':'absolute',
						'cursor':'pointer',
						'left':'286px',
						'top':'30px',
						'font-family':'Comic Sans MS',
						'font-size':'12px',
						'font-weight':'bold',
						'color':'DarkGray'
					});				
					if (IE6) {
						$(span).addClass("closebtn").css({
							'position':'absolute',
							'cursor':'pointer',
							'left':'286px',
							'top':'37px',
							'font-family':'Comic Sans MS',
							'font-size':'12px',
							'font-weight':'bold',
							'color':'DarkGray'
						});
					}if  (IE7) {
					$(span).addClass("closebtn").css({
						'position':'absolute',
						'cursor':'pointer',
						'left':'286px',
						'top':'37px',
						'font-family':'Comic Sans MS',
						'font-size':'12px',
						'font-weight':'bold',
						'color':'DarkGray'
					});
					}if  (Opera) {
					$(span).addClass("closebtn").css({
						'position':'absolute',
						'cursor':'pointer',
						'left':'285px',
						'top':'27px',
						'font-family':'Comic Sans MS',
						'font-size':'12px',
						'font-weight':'bold',
						'color':'DarkGray'
					});
					}
					break;
					
					
					case "342":
					$(span).addClass("closebtn").css({
						'position':'absolute',
						'cursor':'pointer',
						'left':'258px',
						'top':'30px',
						'font-family':'Comic Sans MS',
						'font-size':'12px',
						'font-weight':'bold',
						'color':'DarkGray'
					});				
					if (IE6) {
						$(span).addClass("closebtn").css({
							'position':'absolute',
							'cursor':'pointer',
							'left':'258px',
							'top':'37px',
							'font-family':'Comic Sans MS',
							'font-size':'12px',
							'font-weight':'bold',
							'color':'DarkGray'
						});
					}if  (IE7) {
					$(span).addClass("closebtn").css({
						'position':'absolute',
						'cursor':'pointer',
						'left':'258px',
						'top':'37px',
						'font-family':'Comic Sans MS',
						'font-size':'12px',
						'font-weight':'bold',
						'color':'DarkGray'
					});
					}if  (Opera) {
					$(span).addClass("closebtn").css({
						'position':'absolute',
						'cursor':'pointer',
						'left':'256px',
						'top':'27px',
						'font-family':'Comic Sans MS',
						'font-size':'12px',
						'font-weight':'bold',
						'color':'DarkGray'
					});
					}
					break;

			
			}
			var alink = document.createElement("a");

			alink.innerHTML = (defaultConfig.template_id == '337' || defaultConfig.template_id == '338') 
				? '<img src="http://live-cams-0.livejasmin.com/templates/public/338/images/x.png" alt="close">'
				: 'X';

			span.appendChild(alink);
			alink.onclick = Slider.turnOff;
			this.innerDiv.appendChild(span);
			var iframe = document.createElement("iframe");
			iframe.frameBorder = "no";
			iframe.setAttribute("src", this.config.iframeUrl);
			iframe.setAttribute("scrolling", "no");
			iframe.setAttribute("border", "none"); 
			iframe.style.cssText = "border:none; overflow: hidden; top:" + this.banSizeObj.height +"px; z-index:99998; width:" + this.banSizeObj.width + "px; height:" + this.banSizeObj.height + "px; ";

			this.innerDiv.appendChild(iframe);
		}

		return this.innerDiv;
	},
	
	manageSlide: function()
	{
		if (this.isInited === false) {
			return;	
		}

		if (this.slides == 0 || (this.config.behaviour == "looping" && this.slides % 2 == 0)) {
			this.reloadIframe();
			this.timer = setTimeout("Slider.doSlide('')",1000 * this.config.upDelay);
		}

		if ((this.config.behaviour == "upDown" && this.slides == 1) || (this.config.behaviour == "looping" && this.slides % 2 == 1)) {
			this.timer = setTimeout("Slider.doSlide('down')",1000 * this.config.downDelay);
		}

		this.slides++;
	},

	doSlide: function(direction)
	{
		if (this.isInited === false) {
			return;	
		}

		var x;
		var currPos = parseInt(this.getInnerDiv().style.top);
		this.getContainer().style.display = ''; 

		if (direction === 'down') {
			x = this.getInnerDiv().offsetHeight;

			if (currPos <= x) {
				currPos += (currPos + 5) > x ? ( (x - currPos) +1 ): 5;
				this.getInnerDiv().style.top = currPos + 'px';
				this.timer = setTimeout("Slider.doSlide('down')",30);
			}
			else {
				clearTimeout(this.timer);
				this.getContainer().style.display = 'none'; 
				this.manageSlide();
			}
			this.turnOff();
		}
		else {
			x = 0;
			if (currPos > x) {
				currPos -= currPos < 5? currPos:5;
				this.getInnerDiv().style.top = currPos + 'px';
				this.timer = setTimeout("Slider.doSlide('')",30);
			}
			else {
				clearTimeout(this.timer);
				this.manageSlide();
			}
		}
	},
	
	freechat: function()
	{
		this.reloadIframe();
	},
	
	reloadIframe: function()
	{
		if (this.isInited === false) {
			return;	
		}

		this.getInnerDiv().setAttribute("src", this.config.iframeUrl);
	},
	
	turnOff: function()
	{
/*		this.config.behaviour = "up";
		this.doSlide("down");*/
		document.body.removeChild(Slider.getContainer());
	},

	configToUrl: function()
	{
		var tempUrl = this.config.iframeUrl + '?';
		var paramsArray = [];
		var prefix = (this.config.iframeUrl.indexOf('?') >= 0 ? '&' : '?');

		for (var i = 0; i < this.config.urlParameters.length; i++)
		{
			paramsArray.push(this.config.urlParameters[i] + "=" + this.config[this.config.urlParameters[i]]);
		}

		this.config.iframeUrl += prefix + paramsArray.join("&");
	}

};

function moveSlider()
{
	var winHeight = $(window).height();
	var winTop = $(window).scrollTop();
	var contId = Slider.containerId;
	var topPos = (winHeight + winTop) - $("#"+contId).height() ;
	$("#"+contId).css("top", topPos);
}

/** You can initialize the class like this */
/*
window.onload = function(){
	Slider.init({
		upDelay: 2,
		behaviour: "up",
		downDelay: 2,
		skin: "tv",
		size: "big"
	});
}*/
