var debug = null;
function message(message) {
	if (debug == null) {
	 	$('body').prepend('<div id="debug" style="position:absolute;top:0;left:400px;z-index:1000;background:#000;padding:10px;width:500px;overflow:hidden;"><p><b>DEBUG</b></p></div>');
	 	debug = $('div#debug');
	 	debug.click(function(){
	 		if (debug.height() < 100) {
	 			debug.css('height','auto');
	 		} else {
	 			debug.css('height','10px');
	 		}
	 	
	 	});
	}
	//document.getElementById("message").innerHTML = document.getElementById("message").innerHTML +'<br />'+ message;
	//console.log(message);
	debug.append('<p style="color:#fff">'+message+'</p>');
}


var caption = null;
var captionS = false;
var slides = null;
var strip = null;
var imgElm = null
var mainImg = null
var timeo = null;
var hnotice = null;
var backissues = null;
var hash = null;
var tabs = null;
var login = null;
var loginf = null;
var loginlvl = 2;
var tabs_array = null;
var tabs_active = 0;
var morelike = null;
var morelikes = true;
var morelikev = null;
var morelikevs = true;
var feedbt = null;
var iamap = null;

$(document).ready(function(){
	
	// interactive map
	 iamap = $('div#iamap');
	if (iamap.length > 0) {
		initMap();
	}

	// slideshow
	imgElm = $('.bigPic .lc img');
	caption = $('.bigPic .rc');
	slides = $('.smallPics');
	mainImg = $('.bigPic .lc img');
	if (mainImg.length == 0) {
		mainImg = null;
	}
	if (slides.length > 0) {
		var mainWidth = 640;
		var ul = slides.find('ul');
		ul.css('float','none');
		ul.css('display','block');
		ul.wrap('<div class="thumbswrapper" style="margin:0;padding:0;width:'+mainWidth+'px;overflow:hidden;height:69px;"></div>');
		
		initSlides();
	}

	// first image is video
	var lc = $('div.lc');
	var lcr = lc.attr('rel');
	if (lc.length > 0 && lcr != ',,' && lcr != null) {
		var info = lc.attr('rel').split(',');
			
		h = parseInt((460 / info[0]) * info[1]);
		w = 460;
		
		$('.bigPic .lc').css('height',h+'px');
		mainImg.after('<div id="videoplayer"></div>');
		var previewimg = mainImg.attr('src');
		mainImg.remove();
		mainImg = $('.bigPic .lc #videoplayer');
		
		addFlash(mainImg,info[2],w,h,previewimg)
	}


	// stop pasting
	var stppst = $('input.stoppaste');
	if (stppst.length > 0) {
		 if (typeof stppst[0].onpaste != "function" && !!stppst[0].getAttribute("onpaste")) {
        stppst[0].onpaste = eval("(function () { " + stppst[0].getAttribute("onpaste") + " })");
      }

      if (typeof stppst[0].onpaste == "function") {
        var oninput = stppst[0].oninput;

        stppst[0].oninput = function () {
          if (typeof oninput == "function") {
            oninput.apply(this, arguments);
          }

          if (typeof this.previousValue == "undefined") {
            this.previousValue = this.value;
          }

          var pasted = (Math.abs(this.previousValue.length - this.value.length) > 1 && this.value != "");

          if (pasted && !this.onpaste.apply(this, arguments)) {
            this.value = this.previousValue;
          }

          this.previousValue = this.value;
        };

        if (stppst[0].addEventListener) {
          stppst[0].addEventListener("input", stppst[0].oninput, false);
        } else if (stppst[0].attachEvent) {
          stppst[0].attachEvent("oninput", stppst[0].oninput);
        }
      }
	
	}
	
});

function initMap() {
	
	iamap.find('a').mouseover(function(){
		var top = $('div.mapdetail p.goLeft');
		$(this).attr('rel') != '' ? img = $(this).attr('rel') : img = '/layout/img/empty.gif'
		top.html('<img src="'+img+'" alt="'+$(this).attr('title')+'" width="80" height="60" />');
		top.next().html($(this).html()+'<br />'+$(this).attr('title'))
	});
}


function addFlash(dom,flv,w,h,img) {
	swfobject.switchOffAutoHideShow();
	
	
	var today = new Date();
	var mill = today.getTime();
	if ($.browser.opera) {
	
		var flashvars = {
			videourl: flv,
			w: w,
			h: h,
			img: img
		};
		var params = {
			allowFullScreen: "true",
			scale: "noScale",
			salign: "tl",
			bgcolor: "#ffffff"
		};
		
		swfobject.embedSWF("/layout/flash/videoplayer.swf?"+mill, "player_video", w, h, "9.0.124","/layout/flash/expressInstall.swf", flashvars, params);

	
	} else {
		var qth = h+16;
		
		var video_html = '<object id="swfContent" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+w+'" height="'+h+'"><param name="movie" value="/layout/flash/videoplayer.swf?'+mill+'" /><param name="allowFullScreen" value="true" /><param name="bgcolor" value="#525252" /><param name="scale" value="noScale" /><param name="salign" value="tl" /><param name="flashvars" value="videourl='+flv+'&w='+w+'&h='+h+'&img='+img+'" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="/layout/flash/videoplayer.swf?'+mill+'" width="'+w+'" height="'+h+'"><param name="allowFullScreen" value="true" /><param name="bgcolor" value="#525252" /><param name="scale" value="noScale" /><param name="salign" value="tl" /><param name="flashvars" value="videourl='+flv+'&w='+w+'&h='+h+'&img='+img+'" /><object type="video/mp4" data="'+flv+'" width="'+w+'" height="'+qth+'"><!-- QuickTime, iPhone --><param name="controller" value="true" /><param name="src" value="'+img+'" /><param name="href" value="'+flv+'" /><param name="target" value="myself" /><!--<![endif]--><img src="'+img+'" alt="Video content" /><!-- alternative content --><!--[if !IE]>--></object></object><!--<![endif]--></object>';
		dom.html(video_html);
		swfobject.registerObject("swfContent", "9.0.124","/layout/flash/expressInstall.swf");
	}
		
}


function initSlides() {
	strip = new iStrip(slides);
}


function iStrip(slides) {
	this.me = slides;
	this.strip = $(slides).find('div.thumbswrapper');
	this.container = $(slides).find('ul');
	this.imgs = new Array();
	this.btn = new Array();
	this.lngth = 0;
	this.cpos = 0;
	this.max = null;
	this.maxp = null;
	this.act = [true,true];
	this.slngth = this.strip.width();
	this.crrImg = null;
	this.init();
	this.firstImg();
	this.time = 650;
	this.mode = false;
	this.busy = false;
	return this;
}


iStrip.prototype.init = function() {
	
	// append the main image as an additional image to the strip
	var pressid = this.me.attr('id');
	if (pressid != 'pressthumbs') {
		var lc = $('.bigPic .lc');
		if (lc.attr('rel') != ',,' && lc.attr('rel') != null) {
			values = lc.attr('rel').split(',');
			this.container.prepend('<li><a style="background-image: url('+mainImg.attr('src')+');" class="videothumb" rel="'+values[0]+','+values[1]+','+mainImg.attr('src')+'" href="'+values[2]+'"><img width="22" height="20" title="Play video" alt="Play video button" src="/layout/img/videoThumbOverlay.gif"/></a><div class="hidden"><h3>Movie name</h3><p>movie caption</p></div></li>');
		} else {
			this.container.prepend('<li><a href="'+mainImg.attr('src')+'" sheight="'+mainImg.attr('height')+'" swidth="'+mainImg.attr('width')+'"><img src="'+mainImg.attr('src')+'" height="59" /></a></li>');
		}
	}	
	
	
	// find all images and set container length
	var obj = this;
	this.container.find('a').each(function(){
		obj.addImg($(this));
	});
	this.container.css('width',this.lngth+10);
	this.max = -((this.lngth-10) - this.slngth);
	this.maxp = 0;
	var l = this.imgs.length;
	for(this.maxp=0;this.maxp<l;this.maxp++) {
		if (this.max >= this.imgs[this.maxp][1]) {
			break;
		}
	}

	// attach buttons but only if width is wider than 350 / 650
	if (this.lngth > 640) {
		this.me.prepend('<p><a class="prev inactive" href="#">prev</a><span> | </span><a class="next" href="#">next</a></p>');
	}

	// set up buttons
	this.btn[0] = this.me.find('a.next');
	this.btn[1] = this.me.find('a.prev');
	this.btn[0].click(function(me){me.preventDefault();obj.move(-1);return false;});
	this.btn[1].click(function(me){me.preventDefault();obj.move(1);return false;});
	this.ckB();

	if (pressid == 'pressthumbs') {
		this.thumbClick(0);
	}
	
}

iStrip.prototype.firstImg = function() {
	imgElm.css('cursor','pointer');
	obj = this;
	if (this.imgs[0][3] == "") {
		imgElm.click(function(me){
			me.preventDefault();
			obj.move(1);
			return false;
		});
	} else {
		var url = this.imgs[0][2];
		imgElm.css('cursor','pointer');
		imgElm.click(function(me){
			me.preventDefault();
			document.location.href = url;
			return false;
		});
	}
	this.activateImg(0);

}

iStrip.prototype.activateImg = function(which) {
	if (this.crrImg != null) {
		this.imgs[this.crrImg][0].removeClass('active');
	}
	this.crrImg = which;
	this.imgs[which][0].addClass('active');
}
iStrip.prototype.addImg = function(img) {

	if ($(img).hasClass('videothumb')) {
		var w = $(img).width()+5;
	} else {
		var w = $(img).find('img').width()+5;
	}
	var h = $(img).attr('href');
	var r = $(img).attr('rel');
	var sh = $(img).attr('sheight');
	var sw = $(img).attr('swidth');
	var c = $(img).parent().find('div');
	if (c.length > 0) {
		var c1 = c.find('h3').html();
		var c2 = c.find('p').html();
	} else {
		// get caption from existing one
		var c1 = caption.find('h3').html();
		var c2 = caption.find('p').html();
	}
	
	$(img).hasClass('videothumb') == true ? isvid = 1 : isvid = 0;

	var l = this.imgs.length;
	var obj = this;
	this.imgs.push([img,-this.lngth,h,r,sh,c1,c2,sw,isvid]);
	this.lngth +=  w;

	img.click(function(me){
		me.preventDefault();
		obj.thumbClick(l);
		return false;
	});
}
iStrip.prototype.thumbClick = function(which) {
	this.busy = true;
	//console.log($('#largeplayersb'));
	//$('#largeplayersb').remove();
	var clickable = false;
	var newImg = null;
	//if (this.imgs[which][3] == "") {
	newImg = this.imgs[which][2];
	//} else {
		// we have a video or slide show
	if (this.imgs[which][8] == 1) {
		clickable = true;
		//newImg = this.imgs[which][2];
	}
	caption.find('h3').html(this.imgs[which][5]);
	caption.find('p').html(this.imgs[which][6]);
	
	// here we need to work out wich mode we are in and display either the original height (4) or the 
	// newly calculated height / width (7)
	if (clickable) {
		var imgElmHeight = 380;
		var newWidth = 460;
		var newHeight = 380;
	} else {
		var imgElmHeight = this.imgs[which][4];
		var newWidth = this.imgs[which][7];
		var newHeight = this.imgs[which][4];	
	}

	
	obj = this;
	mainImg.fadeOut('slow',function(){
		//console.log(clickable);
		
		if(clickable) {
			var info = obj.imgs[which][3].split(',');
			
			h = parseInt((460 / info[0]) * info[1]);
			w = 460;
			
			$('.bigPic .lc').css('height',h+'px');
			mainImg.after('<div id="videoplayer"></div>');
			mainImg.remove();
			mainImg = $('.bigPic .lc #videoplayer');
			
			addFlash(mainImg,obj.imgs[which][2],w,h,info[2])
			
			
		} else {
			$('.bigPic .lc').css('height',imgElmHeight+'px');
			imgElm.css('height',imgElmHeight+'px');
			//var targetOffset = imgElm.offset().top;
			//var bodyOffset = $('html').scrollTop() == 0 ? $('body').scrollTop() : $('html').scrollTop();
			//if ((bodyOffset - targetOffset) > 1) {
			//	$('html,body').animate({scrollTop: targetOffset}, 500);
			//}
			//console.log(mainImg);
			mainImg.after('<img width="'+newWidth+'" height="'+newHeight+'" src="'+newImg+'" />');
			mainImg.remove();
			mainImg = $('.bigPic .lc img').not('#largeplayersb');
			mainImg.hide();
			var nh = newHeight;
			mainImg.load(function(){
				var newHeight = nh;
				mainImg.fadeIn('slow',function(){
					//mainImg.css('display','block');
				});	
				
			});
			
		}
		obj.busy = false;
	});	
	this.activateImg(which);
	imgElm.unbind('click');
	imgElm.css('cursor','auto');
	var obj = this;
	if (clickable) {
		var url = this.imgs[which][2];
		imgElm.css('cursor','pointer');
		imgElm.click(function(me){
			me.preventDefault();
			alert('video: '+this.imgs[which][3]);
			//document.location.href = url;
			return false;
		});
	} else {
		imgElm.css('cursor','pointer');
		imgElm.click(function(me){
			me.preventDefault();
			obj.move(1);
			return false;
		});
	
	}
}
iStrip.prototype.getW = function() {
	return this.lngth+10;
}

iStrip.prototype.move = function(dir) {
	//alert('move');
	
	//console.log(this.crrImg);
	if (!this.busy) {
		//imgElm.trigger('mouseout');
		if (dir == 1 && this.crrImg < this.imgs.length) {
			if (this.cpos < this.maxp && this.act[1]) {
				this.cpos++;
				var npos = this.imgs[this.cpos][1];
				if (-(npos) > (this.lngth-this.slngth)) {
					npos = -(this.lngth-this.slngth)+1;	
				}
				this.container.animate({marginLeft:npos},this.time);
			}
			this.thumbClick(this.crrImg+1);
		} else  if (this.crrImg > 0){
			if (this.cpos > 0 && this.act[0]) {
				this.cpos--;
				var npos = this.imgs[this.cpos][1];
				this.container.animate({marginLeft:npos},this.time);
				
			}
			this.thumbClick(this.crrImg-1);
		}	
		this.ckB();
	}
}
iStrip.prototype.ckB = function() {
	if (this.cpos == 0) {
		this.btn[0].addClass('inactive');
		this.act[0] = false;
		if (this.lngth+10 > 650) {
			this.act[1] = true;
			this.btn[1].removeClass('inactive');
		} else {
			this.act[0] = false;
			this.btn[0].addClass('inactive');		
		}
	} else if (this.cpos == this.maxp) {
		this.btn[1].addClass('inactive');
		this.act[1] = false;
		this.act[0] = true;
		this.btn[0].removeClass('inactive');
	} else {
		this.btn[0].removeClass('inactive');
		this.btn[1].removeClass('inactive');
		this.act[0] = true;
		this.act[1] = true;
	}
}