var im1;
var im2;
var im3;
var im4;
var clipid;

$(document).ready(function(){
   
   vote();
   thumbs();
   publish();
   friend();
   //slide();
   vehicles();  
   comment();
   showcomments();
     
});
 
function showcomments() {
	
	//$("#showcomments").click(function (event) {
		
	//event.preventDefault();
	
	$("#message").val("Please enter your comment.");
	$("#message").click(function (event) {
		
		$("#message").val("");
		
	});
	
	//$(".commenthide").show();	
		
	//});
	
	$("#showcomments").toggle(function(){
     $(".commenthide").show();
	 //$("#showcomments").text("[Hide]");
   },function(){
     $(".commenthide").hide();
	 //$("#showcomments").text("[Show all comments]");
   });
	
	
}

function comment() {
	
	$("#addcomment").submit(function (event) {
		event.preventDefault();
	
		if ($("#message").val() == "" || $("#message").val() == "Please enter your comment.") {
			$("#error").show();
		} else {
		
		var data = "id="+$("#movieid").val()+"&message="+$("#message").val();
	
		$.ajax({
			type: "POST",
			url: "/comment",
			data: data,
			dataType: "json",
			success: function(msg) {
				
				if (msg) {
				
					if (document.getElementById("nocomments")) { //first comment
						$("#nocomments").remove();
						$("#commentsend").before("<p class="+'"commentshow"'+"><strong class="+'"name"'+">" + msg.firstname + " " + msg.lastname + ":</strong><strong> (" + msg.date + ")</strong></p>" + "<p class="+'"commentshow"'+">" + msg.comment + "</p>");						
					} else {
						
						
						//$("#showcomments").text("[Hide]");
						$("#commentsend").before("<p class="+'"commenthide"'+"><strong class="+'"name"'+">" + msg.firstname + " " + msg.lastname + ":</strong><strong> (" + msg.date + ")</strong></p>" + "<p class="+'"commenthide"'+">" + msg.comment + "</p>");
						
						
						//alert($(".commenthide").css("display"));
						//if ($(".commenthide").css("display") == "none") {
							$("#showcomments").unbind();
							$("#showcomments").toggle(function(){							
								$(".commenthide").hide();
								//$("#showcomments").text("[Show all comments]");
							},function(){
								$(".commenthide").show();
								//$("#showcomments").text("[Hide]");
							});
						//}
						$(".commenthide").show();
					}
					$("#message").val("");
					$("#error").hide();
					$("#charcounter").text("150");
				
				}
								
			},
			error: function(xml, msg) {
				alert("Sorry, an error occurred.");
			}
		});	
		
		}
		
	});
}

function slide() {
	
	$("#slidetestup").click(function (event) {
	
	event.preventDefault();
		$("#comments").hide("slow");
		//$("#some5").show("slow");
	}
	);
	$("#slidetestdown").click(function (event) {
	
	event.preventDefault();
		$("#comments").show("slow");
		//$("#some5").hide("slow");
	}
	);
	
}



function vehicles() {
		
	$("#vehicles-redirect").click(function() {
	
	var currentlink = $("#vehicles-redirect").attr("href");
	urchinTracker(currentlink);
		
	});
	
	$("#vehicles-fox").click(function (event) {
		event.preventDefault();
		$("#vehicles-large").attr("src", "/_images/fox.jpg");
		$("#vehicles-redirect").attr("href", "/_redirects/fox.php");
		$("#vehicles-redirect").attr("class", "fox");
		$(".vehicles-text").hide();
		$("#vehicles-text-fox").show();
		
		$("#vehicles-title-vwandfilm").attr("class", "vehicles-title-fox");
	});
	$("#vehicles-golf").click(function (event) {
		event.preventDefault();
		$("#vehicles-large").attr("src", "/_images/golf.jpg");
		$("#vehicles-redirect").attr("href", "/_redirects/golf.php");
		$("#vehicles-redirect").attr("class", "golf");
		$(".vehicles-text").hide();
		$("#vehicles-text-golf").show();
		
		$("#vehicles-title-vwandfilm").attr("class", "vehicles-title-golf");
	});
	$("#vehicles-beetle").click(function (event) {
		event.preventDefault();
		$("#vehicles-large").attr("src", "/_images/beetle.jpg");
		$("#vehicles-redirect").attr("href", "/_redirects/beetle.php");
		$("#vehicles-redirect").attr("class", "beetle");
		$(".vehicles-text").hide();
		$("#vehicles-text-beetle").show();
		
		$("#vehicles-title-vwandfilm").attr("class", "vehicles-title-beetle");
	});
	$("#vehicles-touran").click(function (event) {
		event.preventDefault();
		$("#vehicles-large").attr("src", "/_images/touran.jpg");
		$("#vehicles-redirect").attr("href", "/_redirects/touran.php");
		$("#vehicles-redirect").attr("class", "touran");
		$(".vehicles-text").hide();
		$("#vehicles-text-touran").show();
		
		$("#vehicles-title-vwandfilm").attr("class", "vehicles-title-touran");
	});
	$("#vehicles-touareg").click(function (event) {
		event.preventDefault();
		$("#vehicles-large").attr("src", "/_images/touareg.jpg");
		$("#vehicles-redirect").attr("href", "/_redirects/touareg.php");
		$("#vehicles-redirect").attr("class", "touareg");
		$(".vehicles-text").hide();
		$("#vehicles-text-touareg").show();
		
		$("#vehicles-title-vwandfilm").attr("class", "vehicles-title-touareg");
	});
	$("#vehicles-phaeton").click(function (event) {
		event.preventDefault();
		$("#vehicles-large").attr("src", "/_images/phaeton.jpg");
		$("#vehicles-redirect").attr("href", "/_redirects/phaeton.php");
		$("#vehicles-redirect").attr("class", "phaeton");
		$(".vehicles-text").hide();
		$("#vehicles-text-phaeton").show();
		
		$("#vehicles-title-vwandfilm").attr("class", "vehicles-title-phaeton");
	});
			
	
}
function friend() {
	
	$("#friend").submit(function (event) {
		event.preventDefault();
	
		$("#pop-up").show();
	
	});
	
	$("#pop-up").submit(function (event) {
		event.preventDefault();
	});
	
	$("#send").click(function (event) {
		
		
		if (trim($("#femail").val()) == "" ||
			trim($("#yname").val()) == "" ||
			trim($("#fname").val()) == "" ||
			trim($("#message").val()) == "" ) {
			$("#frienderroremail").hide();
			$("#frienderrormissing").show();
		} else {
		
		var data = "clipid="+$("#clipid").val()+"&femail="+$("#femail").val()+"&yname="+$("#yname").val()+"&fname="+$("#fname").val()+"&message="+$("#message").val();	
			
		$.ajax({
			type: "POST",
			url: "/sendfriend",
			data: data,
			success: function(msg) {
				
				if (msg == "true") {
					$("#femail").val("");
					$("#yname").val("");
					$("#fname").val("");
					$("#message").val("");
					$("#frienderroremail").hide();
					$("#pop-up").hide();
					urchinTracker('/sendtoafriend');
				} else if (msg == "false") {
					$("#frienderrormissing").hide();
					$("#frienderroremail").show();
				}
				
				
			},
			error: function(xml, msg) {
				alert("Sorry, an error occurred.");
			}
		});	
					
		}
		
	});
		
	$("#cancel").click(function (event) {
						
		$("#yemail").val("");
		$("#femail").val("");
		$("#yname").val("");
		$("#fname").val("");
		$("#message").val("");
		$("#frienderrormissing").hide();
		$("#frienderroremail").hide();
		$("#pop-up").hide();
	});
	
}


function publish() {
	
	$("#publisherror").hide();
	$(".publishlinks").click(function (event) {
		event.preventDefault();
		clipid = $(event.target).attr("id");
		var title = $("#cliptitle"+clipid).text();
		
		$("#publishtitle").text('"'+title+'"');
		$("#pop-up").show();
			
	});
	
	$("#publishcancel").click(function (event) {
		event.preventDefault();
		$("#pop-up").hide();
		$("#publisherror").hide();
		$("#accepted").removeAttr("checked");
		
	});
	
	$("#publishsubmit").click(function (event) {
		event.preventDefault();
		var terms = $("#accepted").attr("checked");
		
		if (terms) {
		
			var data = "clipid="+clipid;
			
			$.ajax({
			type: "POST",
			url: "/publish",
			data: data,
			success: function(msg) {
				
				if (msg != "true") {
					
					$("#p"+clipid).empty();
					$("#p"+clipid).text("Yes");
					$("#pop-up").hide();
					$("#publisherror").hide();
					$("#accepted").removeAttr("checked");
				}
			},
			error: function(xml, msg) {
				alert("Sorry, an error occurred.");
			}
			});
				
		} else {
			$("#publisherror").show();
		}
		
	});
	 
}


function thumbs() {
	
	$("#thumbleft").click(function (event) {
		event.preventDefault();
		
		var startlink = $("#piclink0").attr("href");
		var startid = startlink.substr(18);
		var startidindex;
		
		for (var x in imageIds) {
			if (imageIds[x] == startid) {
				startidindex = x;
				startidindex -= 0; // convert to number
			}
			
		}
		
		var end = imageIds.length -3;
		
		if (imageIds.length < 4) return false;
		if (startidindex == end) return false;
				
		im1 = images[startidindex+1];
		im2 = images[startidindex+2];
		im3 = images[startidindex+3];
				
		imageUpdate();                            
		
	});
	
	$("#thumbright").click(function (event) {
		event.preventDefault();
		
		var startlink = $("#piclink0").attr("href");
		var startid = startlink.substr(18);
		var startidindex;
		
		for (var x in imageIds) {
			if (imageIds[x] == startid) {
				startidindex = x;
				startidindex -= 0; // convert to number
			}
			
		}
		if (imageIds.length < 4) return false;
		if (startidindex == 0) return false;
		
		im1 = images[startidindex-1];
		im2 = images[startidindex];
		im3 = images[startidindex+1];
				
		imageUpdate();                            
		
	});
	 
}


function imageUpdate() {
	
	$("#piclink0").attr({href: "/simulator/player/"+im1[0]});
		$("#picimg0").attr({src: "/_images/stunts/"+im1[0]+".png", alt: "Stunt Image "+im1[0]});
		$("#pictitle0").html(im1[1]);
		$("#picname0").text(im1[2]);
		$("#picadded0").text(im1[3]);
		$("#picviews0").text(im1[5]);
		$("#picvotes0").text(im1[4]);
		
		$("#piclink1").attr({href: "/simulator/player/"+im2[0]});
		$("#picimg1").attr({src: "/_images/stunts/"+im2[0]+".png", alt: "Stunt Image "+im2[0]});
		$("#pictitle1").html(im2[1]);
		$("#picname1").text(im2[2]);
		$("#picadded1").text(im2[3]);
		$("#picviews1").text(im2[5]);
		$("#picvotes1").text(im2[4]);
		
		$("#piclink2").attr({href: "/simulator/player/"+im3[0]});
		$("#picimg2").attr({src: "/_images/stunts/"+im3[0]+".png", alt: "Stunt Image "+im3[0]});
		$("#pictitle2").html(im3[1]);
		$("#picname2").text(im3[2]);
		$("#picadded2").text(im3[3]);
		$("#picviews2").text(im3[5]);
		$("#picvotes2").text(im3[4]);
				
}

 
function vote() {

$("#vote").submit(function(event) {
	
	event.preventDefault();
	var user = readCookie("user");
	
	if (!user) {
		window.location = "/login/";	
	} else {
	
	var movieid = $("#movieid").val();
	var data = "user=" + user + "&movieid=" + movieid;
			
	$.ajax({
			type: "POST",
			url: "/vote",
			data: data,
			success: function(msg) {
				
				if (msg != "false") {
					$("#votenum").text(msg);
					$("#ratethisclip").attr("src", "/_images/button_rate-this-clip_tick.gif");
				}
			},
			error: function(xml, msg) {
				alert("Sorry, an error occurred.");
			}
		});
		
	}
	
});
		
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function createCookie(u,p) {
	//var date = new Date();
	//date.setTime(date.getTime()+(7*24*60*60*1000));
	//var expires = "; expires="+date.toGMTString();
		
	//document.cookie = "user="+u+expires+"; path=/";
	//document.cookie = "pw="+p+expires+"; path=/";
	$("#user").text("Hi "+u);		
	document.cookie = "user="+u+"; path=/";
	document.cookie = "pw="+p+"; path=/";
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
