var WLD = WLD || {};
WLD.Public = WLD.Public || {};

WLD.Public.Transition = Class.create({

	initialize : function(banners, imagePath, frame1, frame2, nav, delay, fade, auto, txt, debug)
	{
	  
		this.myBanners = banners;
		this.imagePath = imagePath;
		this.secondsDelay = delay;
		this.fadeDelay = fade;
		this.frame1 = 0;
		this.frame2 = 0;
		this.debug = debug;
		this.timer = "";
		this.auto = auto;
		this.frame1Name = frame1;
		this.frame2Name = frame2;
		this.nav = nav;
		this.txt = txt;

		for(var x = 0; x < this.myBanners.length; x++)
		{
			var tempAsset = new Image();
			tempAsset.src = this.imagePath + this.myBanners[x];
			this.myBanners[x] = tempAsset;
			//this.myBanners[x].onload = this.al(this.myBanners.length, x);
			this.al(this.myBanners.length, x);
		}
	},

	al : function(total, current)
	{
		if(total == (current+1)) this.go();
	},

	go : function()
	{
		this.assignFrameValues();
		this.assignFrame(this.frame2Name, this.frame2);
		this.assignFrame(this.frame1Name, this.frame1);

		if(this.nav)
		{
			this.showText(this.frame1);
			this.showCurrentButton();
		}
		if(this.auto)
			setTimeout(function(){this.fadeAndShow(false)}.bind(this), this.secondsDelay*1000);
	},

	fadeAndShow : function(cancelTimer)
	{
	  
		Effect.Fade(this.frame1Name, {
			duration : this.fadeDelay,
			from : 1,
			to : 0,
			afterFinish : function() {
				this.assignFrame(this.frame1Name, this.frame2);
				if(this.txt)
					this.showText(this.frame2);
				this.assignFrameValues();
				this.assignFrame(this.frame2Name, this.frame2);
				if(this.nav)
					this.showCurrentButton();
				if(this.auto)
					this.timer = setTimeout(function(){this.fadeAndShow(cancelTimer)}.bind(this), this.secondsDelay*1000);
			}.bind(this)
		});

	},

	assignFrameValues : function()
	{
		if(this.frame1 == 0 && this.frame2 == 0)
		{
			i = Math.floor(Math.random()*this.myBanners.length);
			//i=0;
			this.frame1 = i;
			(i < (this.myBanners.length-1)) ? i++ : i=0;
			this.frame2 = i;
			//this.frame2 = 1;
		}
		else
		{
			i = this.frame2;
			this.frame1 = this.frame2;
			(i < (this.myBanners.length-1)) ? i++ : i=0;
			this.frame2 = i;
		}
	},

	assignFrame : function(frame, value)
	{
		$(frame).setStyle({
			backgroundImage: 'url('+this.myBanners[value].src+')',
			backgroundColor: 'white',
			display: 'block'
		});
	},

	clearFrame : function(frame)
	{
		$(frame).setStyle({
			backgroundImage: 'none',
			backgroundColor: 'transparent'
		});
	},

	showCurrentButton : function(num)
	{
		return true;
		$$('.flash-option').each(function(item){
			$(item).removeClassName('flash-option-active');
		});
		var f2 = 'flash-option-'+ this.frame1;
		$(f2).addClassName('flash-option-active');
	},

	selectSpecific : function(num)
	{
		clearTimeout(this.timer);
		this.frame2 = num;
		this.assignFrame(this.frame2Name, this.frame2);

		Effect.Fade(this.frame1Name, {
			duration : this.fadeDelay,
			from : 1,
			to : 0,
			afterFinish : function() {
				this.showText(this.frame2);
				this.assignFrame(this.frame1Name, this.frame2);
				this.assignFrameValues();
				if(this.nav)
					this.showCurrentButton(num);
			}.bind(this)
		});
		return false;
	},

	showText : function(num)
	{
		var cpy = 'item'+num+'-copy';

		if($(cpy))
		{
			Effect.Fade('flash-copy', {
				duration : this.fadeDelay,
				from : 1,
				to : 0,
				afterFinish : function() {
					$('flash-copy').innerHTML = $(cpy).innerHTML;
					Effect.Appear('flash-copy', {
						duration : this.fadeDelay,
						from : 0,
						to : 1,
						afterFinish : function() {
						}.bind(this)
					});
				}.bind(this)
			});
		}
	}

});

WLD.Public.Video = Class.create({

	initialize : function(path, width, height, divid)
	{
		if (swfobject.hasFlashPlayerVersion("6.0.0"))
		{
			var flashvars = false;
			var params = { play: "false", bgcolor : "#ffffff" };
			var attributes = { bgcolor : "#ffffff" };
			swfobject.embedSWF(path, divid, width, height, "6.0.0", "expressInstall.swf", flashvars, params, attributes);
		}
	}
});

WLD.Public.Preview = Class.create({

	initialize : function(name)
	{
		this.name = name;
		this.imagePath = '/images/partners/casestudies/'+name+'-preview.jpg';
	},

	open : function(event)
	{
		Event.stopObserving(event);
		if(!$('preview-pane'))
		{
			// create the image element
			var im = new Element('img', { src : this.imagePath, alt : this.name });
			// create the close button element
			var exit = new Element('img', { src : '/images/close.gif', alt : 'Close', 'class' : 'cls' });
			var exitlink = new Element('a', { href : '#' });
			exitlink.observe('click', function(event){
				this.close(event);
			}.bind(this));

			exitlink.insert({ top: exit });

			// create the holding element
			var holder = new Element('div', { 'class' : 'preview-holder', id : 'preview-pane' });

			var top = new Element('div', { 'class' : 'preview-top has-icon' });
			var left = new Element('div', { 'class' : 'preview-left has-icon' });
			var bottom = new Element('div', { 'class' : 'preview-bottom has-icon' });
			var right = new Element('div', { 'class' : 'preview-right has-icon' });

			// add the borders
			holder.appendChild(top);
			holder.appendChild(left);
			holder.appendChild(bottom);
			holder.appendChild(right);

			// add elements to holder
			holder.appendChild(im);
			holder.appendChild(exitlink);
			// show the whole lot
			$('content-view').insert({ top: holder });
		}
		return false;
	},

	close : function(event)
	{
		if($('preview-pane'))
			$('preview-pane').remove();
	}

});

WLD.Public.Special = Class.create({

  initialize : function() {
    var c = new WLD.Public.Cookie;
    if(!c.find('wld_kcode')) {
      document.observe('keydown', this.detect.bindAsEventListener(this));
      this.combo = new Array();
      this.sc = Array("38","38","40","40","37","39","37","39","66","65");
    }
    else
      this.alterPage();
  },

  detect : function(event) {
    this.combo.push(event.keyCode);
    if(event.keyCode == this.sc.last()) this.checkCombo();
  },

  checkCombo : function() {
    if(this.combo.toString() == this.sc.toString()) {
      cook = new WLD.Public.Cookie('wld_kcode', 'you have unlocked the WhiteLabelDating website', 1);
      this.alterPage();
    }
    this.combo = new Array();
  },

  alterPage : function() {
    $$('.sp').each(function(itm) {
      itm.src = itm.src.replace('large', 'alt');
    });
  }

});

WLD.Public.Cookie = Class.create({

  initialize : function(name,value,expiredays) {
    if(name != null) {
      var exdate = new Date();
      exdate.setDate(exdate.getDate()+expiredays);
      document.cookie = name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString() + ";path=/");
      return true;
    }
  },

  find : function(name) {
    if (document.cookie.length>0) {
      var c_start=document.cookie.indexOf(name + "=");
      if (c_start!=-1)
        return true;
    }
    return false;
  },

  check : function(name) {
    var username=getCookie('username');
    return (username.length > 0) ? true : false;
  }

});

WLD.Public.ShowCase = Class.create({
  
  initialize: function() {
    
    new Effect.Fade($('pane_contents_holder'), {
      duration: 0.5,
      afterFinish:function() {
        $('pane_contents_holder').addClassName('noshow');
        $('showcase-holder').removeClassName('noshow');
      }
    });
    this.current_set = 1;
    $('content-holder-inner').hide();
  	var i = 1;
		$$('.content_thumb').each(function(item) {
			item.observe('click', this.showContent.bindAsEventListener(this));
			item.id = "thumb_"+i.toString();
			i++;
		}.bind(this));
		this.max_set = $$('.set').size();
		this.handleMoveButtons(true);
	  $('screenie').addClassName('screenie');
	  var elem_to_show_first = $$('.active')[0].id.replace('thumb_', 'content_');
	  var s = function() { this.renderContent(elem_to_show_first); }.bind(this);
		setTimeout(s, 1500);
  },
  moveToNextSet: function() {
    this.handleMoveButtons(false); // switch off the move buttons
		(this.current_set == this.max_set) ? this.moveToSet(1) : this.moveToSet(this.current_set+1);
		return false;
	},
	moveToPreviousSet: function() {
	  this.handleMoveButtons(false); // switch off the move buttons
		(this.current_set == 1) ? this.moveToSet(this.max_set) : this.moveToSet(this.current_set-1);
		return false;
	},
	moveToSet: function(s) {
		var setName = "set"+s.toString();
		var w = Math.round($(setName).getStyle('width').replace('px', ''));
		var lef = 0;
		if($('list').getStyle('left')) lef = Math.round($('list').getStyle('left').replace('px',''));
		var pxl_to_move_to = (lef + (((s * w) - w))) * -1;
		new Effect.Move($('list'), { 
		  x: pxl_to_move_to,
		  y: 0,
		  afterFinish: function() {
		    this.handleMoveButtons(true);
		  }.bind(this)
		});
		this.current_set = s;
	},
	handleMoveButtons: function(switchOn) {
	  if(switchOn) {
	    if($('button-right')) $('button-right').observe('click', this.moveToNextSet.bindAsEventListener(this));
  	  if($('button-left'))  $('button-left').observe('click', this.moveToPreviousSet.bindAsEventListener(this));
	  }
	  else {
	    if($('button-right')) $('button-right').stopObserving('click');
  	  if($('button-left'))  $('button-left').stopObserving('click');
	  }
	  return true;
	},
	showContent: function(event) {
	  $('content-holder-inner').hide();
		var id = Event.element(event).id.replace("thumb_", 'content_');
    if(!$('content_holder').hasClassName('loading')) $('content_holder').addClassName('loading');
		var s = function() { this.renderContent(id); this.showCurrentThumb(Event.element(event)); }.bind(this);
		setTimeout(s, 1500);
		return false;
	},
	renderContent: function(id) {
	  if($('content_holder').hasClassName('loading')) $('content_holder').removeClassName('loading');
		if($(id)) this.accumalateContent(id);
		else alert('Content could not be found!');
		Effect.Appear($('content-holder-inner'));
		return true;
	},
	accumalateContent: function(id) {
	  var par = $(id);
	  $('cpy-screenie').src = par.select('img')[0].src;
	  $('cpy-title').innerHTML = par.select('h4')[0].innerHTML;
	  $('cpy-body').innerHTML = par.select('p')[0].innerHTML;
	  return true;
	},
	showCurrentThumb: function(ele) {
    var class_name = 'active';
	  $$('.content_thumb').each(function(item) {
	    if(item.hasClassName(class_name)) item.removeClassName(class_name);
	  });
	  ele.addClassName(class_name);
	  return true;
	}
});