//using the cartesian co-ordianate system makes it intuitive (to me at least)
var s_position=[];
	s_position['1']="topLeft";
	s_position['2']="topRight";
	s_position['3']="bottomRight";
	s_position['4']="bottomLeft";
	
var s_images=[];
var s_media=[];
var s_slideshow=[];
var s_timer;
var s_index=0;
var s_fadeDelay=500;
var s_slideDelay=7000;
var s_slideLock=false;
$(document).ready(function(){
	s_init();
});

function s_init(){
	var first=true;
	var slideBox=$("<div>",{
		id:"slideControlsBox"
	});
	$("#switch").append(slideBox);
	$("#loadingBox").show();
	$.ajax({
		url:"php/ajaxWrapper.php",
		type:"post",
		data:"type=getNews",
		dataType:"json",
		success:function(data){
			try{
				if(notNull(data.error)){
					alert(data.error);
					return true;
				}
			}catch(e){}
			//cache and set archive info
			var first=true; var count=0;
			for(var ind in data){
				var nid=data[ind]["news"].id;
				s_images[nid]=data[ind]["news"];
				s_slideshow.push(nid);
				var mediaArr=[];
					if(notNull(data[ind]["m1"])) mediaArr.push(data[ind]["m1"]);
					if(notNull(data[ind]["m2"])) mediaArr.push(data[ind]["m2"]);
					if(notNull(data[ind]["m3"])) mediaArr.push(data[ind]["m3"]); 
		        s_media[nid]=mediaArr;
				//create the card
				var div=$("<div></div>",{
					id:"switch_"+nid
				}).addClass("cardBox");
				div.css("color",s_images[nid].c_text_colour);
				//create the card image
				var back=$("<img></img>",{
					src:"http://www.alteregopost.com/uploads/"+s_images[nid].big_pic
				}).addClass("cardImage");
				if(!first) div.css("display","none");
				
				//create the text blocks
				var titleBox=createArticleTitle(nid);
				if(first) titleBox.css("display","block");
				var textBox=createFullArticle(nid);
				
				titleBox.data("id",nid);
				titleBox.click(titleBoxClick);
				
				$("#sliderTitleBox").append(titleBox);
				div.append(textBox);
				div.append(back);
				$("#switchInternal").prepend(div);
				
				//create the card's slide dot
				var source= (first)? "img/slide_dot_on.png": "img/slide_dot_off.png";
				
				var img=$("<img></img>",{
					src:source
				}).addClass("sliderButton").data("id",count).click(function(){
					if(s_slideLock) return true;
					oldid=s_index;
					s_index=parseInt($(this).data("id"));
					s_transition(oldid,s_index);
				});	
				$("#slideControlsBox").append(img);
				first=false;
				count++;
			}
			try{
				$(".articleTextBody").jScrollPane({
					showArrows: true
				});
			}catch(e){/*IE killer*/}
			//moved the article display function down here cuz it's a bitch to read when it's with the dom creation
			function titleBoxClick(){
				var cont=$("#switch_"+$(this).data("id")+":visible");
				$(".articleTextBody").each(function(){
					try{
					var api=$(this).data("jsp");
					api.reinitialise({
						showArrows: true
					});
					}catch(e){/*IE killer*/}
				});
				if($(this).data("visible")=="0"){
					if($(this).data("xAxis")=="0"){
						cont.animate({
							"left":"-44%"
						});
						cont.find(".articleTextBox").animate({
							"right":"-44%"
						});
					}else{
						cont.animate({
							"right":"-44%"
						});
						cont.find(".articleTextBox").animate({
							"left":"-44%"
						});
					}
					$(this).find(".s_tb_title").fadeOut("fast");
					//stop the slideshow
					clearTimeout(s_timer);
					$(this).find(".s_tb_icon").attr("src","img/iris_animate_open.gif");
					$(this).data("visible","1");
				}else{
					if($(this).data("xAxis")=="0"){
						cont.animate({
							"left":"0%"
						});
						cont.find(".articleTextBox").animate({
							"right":"-62%"
						});
					}else{
						cont.animate({
							"right":"0%"
						});
						cont.find(".articleTextBox").animate({	
							"left":"-62%"
						});
					}
					$(this).find(".s_tb_title").fadeIn("fast");
					//resume the slideshow
					s_timer=setTimeout(s_recursive,s_slideDelay);
					$(this).find(".s_tb_icon").attr("src","img/iris_animate_close.gif");
					$(this).data("visible","0");
				}
			}
		},
		error:function(e){
			//alert("Crap happened. "+e.responseText);
		}
	});
}

function s_recursive(){
	oldid=s_index;
	s_index= (s_index>=(s_slideshow.length-1))? 0 : parseInt(s_index+1);
	if(oldid!=s_index) s_transition(oldid,s_index);
}

function s_transition(oldi,newi){
	s_slideLock=true;
	clearTimeout(s_timer);
	var oldid=s_slideshow[oldi];
	var newid=s_slideshow[newi];
	$("#switch_"+oldid).fadeOut(s_fadeDelay,function(){
		$("#slideControlsBox img").each(function(k,item){
			if($(this).data("id")==newi) $(this).attr("src","img/slide_dot_on.png");
		});	
		$("#switch_"+newid).fadeIn(s_fadeDelay,function(){
			s_slideLock=false;
		});
		s_timer=setTimeout(s_recursive,s_slideDelay);
	});
	
	$("#slideControlsBox img").each(function(k,item){
		$(this).attr("src","img/slide_dot_off.png");
	});
	
	$("#switch_control_"+oldid).fadeOut(s_fadeDelay,function(){
		$("#switch_control_"+newid).fadeIn(s_fadeDelay);
	});
	
	
	var contold=$("#switch_controls_"+oldid).find(".s_tb_icon").attr("src","img/iris_animate_close.gif");
	$(".s_tb_container").each(function(){
		var targ=$("#switch_"+$(this).data("id")+":visible");
		if($(this).data("visible")=="1") $(this).find(".s_tb_icon").attr("src","img/iris_animate_close.gif");
		
		if($(this).data("xAxis")=="0"){
			targ.animate({
				"left":"0%"
			});
			targ.find(".articleTextBox").animate({
				"right":"-62%"
			});
		}else{
			targ.animate({
				"right":"0%"
			});
			targ.find(".articleTextBox").animate({	
				"left":"-62%"
			});
		}
		$(this).find(".s_tb_title").fadeIn("fast");
		$(this).data("visible","0");
	});
}

function createArticleTitle(id,arc){
	var fullid= (arc)? "arc_switch_control_"+id : "switch_control_"+id;
	var cont=$("<div>",{id:fullid}).addClass("s_tb_container").addClass(s_position[s_images[id].title_position]);
	cont.data("visible","0");
	
	//icon
	var right=$("<div>");
	var fullimg= (arc)? "img/iris_open.png": "img/iris_closed.png";
	var img=$("<img>",{src:fullimg}).addClass("s_tb_icon");
	
	//title
	var left=$("<div>");
	var table=$("<table>",{css:{"width":"100%","height":"100%"}});
		var tr=$("<tr>");
		var td=$("<td>").addClass("s_tb_title");
		td.css("color",s_images[id].c_text_colour);
		
	var fulltitle= (arc)? "return to search": s_images[id].title;	
	var titleDiv=$("<div>").html(fulltitle);
	td.html(titleDiv);
	tr.append(td);
	table.append(tr);
	
	if(s_images[id].title_position=="1" || s_images[id].title_position=="4"){
		cont.data("xAxis",0);
		left.append(img);
		left.addClass("s_tb_small floatLeft");
		
		right.append(table);
		right.addClass("s_tb_large floatRight")
	}else{
		cont.data("xAxis",1);
		right.append(img);
		right.addClass("s_tb_small floatRight");
		
		left.append(table);
		left.addClass("s_tb_large floatLeft");
	}
	
	cont.append(left);
	cont.append(right);
	return cont;
}

function createFullArticle(id){
	var textLoc= (s_images[id].title_position==1 || s_images[id].title_position==4)? "fullTextRight": "fullTextLeft";
	var textBox=$("<div></div>").addClass("articleTextBox "+textLoc);//+" lightText");//+s_textColor[s_images[ind].text_color])
	textBox.css("backgroundColor",s_images[id].c_bg_colour);
	var wrapper=$("<div>",{
		css:{
			"position":"relative",
			"height":"100%",
			"width":"100%"
		}
	});
	var header=$("<header></header>");
		var title=$("<div>").addClass("articleTitle").html(s_images[id].title);
		date=parseDate(s_images[id].creation_date);
		var date=$("<div>").addClass("articleDate").html(date);
	header.append(title);
	header.append(date);
		
	var art=$("<article></article>").addClass("articleTextBody").html(s_images[id].article);
	var footer=$("<footer></footer>").addClass("articleFooter");
	
	for(var x in s_media[id]){
		var img=s_createNode(id,x);
		footer.append(img);
	}
	
	wrapper.append(header);
	wrapper.append(art);
	wrapper.append(footer);
	textBox.append(wrapper);
	return textBox;
	
	function s_createNode(id,mid){
		var media=s_media[id][mid];
		var stretch=$("<div>").addClass("stretch");
		var sc=$("<div>").addClass("stretchContent");
			sc.data("id",media.id);
		var img=$("<img></img>",{src:"http://www.alteregopost.com/uploads/"+media.thumbnail}).addClass("stretchImg");
		
		sc.bind("click",createMovie);
		sc.hover(function(){
			$(this).find(".stretchImg").css("borderStyle","solid");
			$(this).find(".stretchImg").css("borderColor","#FFF");
			$(this).find(".stretchImg").css({
				"borderWidth":"3px",
				"left":"-3px",
				"bottom":"-3px"
			});
		},function(){
			$(this).find(".stretchImg").css({
				"borderWidth":"0px",
				"left":"0px",
				"bottom":"0px"
			});
		});
		var st=$("<div>").addClass("stretchText");
			var div=$("<div>").html(media.company+"<br>"+media.title);
		st.append(div);
		
		sc.append(img);
		sc.append(st);
		stretch.append(sc);
		return stretch;
	}
}

function s_onHover(){
	$(this).css({
		"borderColor":"#FFF",
		"borderStyle":"solid",
		"borderWidth":"3px"
	});
}
function s_offHover(){
	$(this).css({
		"borderWidth":"0px"
	});
}

function s_createMovie(){
	var id=$(this).data("id");
	$.ajax({
		url:"php/ajaxWrapper.php",
		type:"get",
		data:"param="+id,
		dataType:"json",
		success:function(data){
			if(notNull(data.error)){
				alert("error: "+data.error);
				return true;
			}
			$("#theatreClose").hide();
			$("#lightbox").fadeIn("fast",function(){
				var flv=data.flv;
				var ogg=data.ogg;
				var width=data.width;
				var height=data.height;
				var htmlHeight=parseInt(height)+35;
				$("#theatre").animate({
					"height":height+"px",
					"marginTop":"-"+parseInt(htmlHeight/2)+"px",
					"width":width+"px",
					"marginLeft":"-"+parseInt(width/2)+"px"
				},function(){
					//the duplicated flash html is for FF since it won't treat the object as an alternate source
					//left it in place for IE and anything that can't use h.264 but can use flash
					$("#screen").html('<object width="'+width+'" height="'+htmlHeight+'" type="application/x-shockwave-flash" data="videoplayer.swf?path='+flv+'&width='+width+'&height='+height+'">'+
							'<param name="allowFullScreen" value="true" />'+	
							'<param name="movie" value="videoplayer.swf?path='+flv+'&width='+width+'&height='+height+'" />'+
						'</object>');
				});
			});
		},
		error:function(data){
			alert("server error: "+data.responseText);
		}
	});
}
