var increment = 980;
var increment2 = 920;
var count = 0;
var controller = $('video');

function slideLeft(){
count = count+1;
if (count <= 6){	
$('#wide-container').animate({"left":"-=563px"},400);
}
else {	
$('#wide-container').animate({"left":"0px"},400);
count = 0;
}
setTimeout("slideLeft()",7000);
}

function workDrop(){
	var workLeft = $(".work").offset().left;
	$("#work-drop").css({"left":workLeft});
}

function black() {

	
	// $('html').css({"overflow":"hidden"});
	$('html, body').animate({scrollTop: '0px'}, {
			duration: 400,
			complete: function(){
					$('html').css({"overflow":"hidden"});
			}
	});
	$('#black-bg').css({"z-index":"100"})
						 	  .animate({"opacity":"0.60"},500);
	$('#black-front').css({"z-index":"101"})
								.animate({"height":"70%"},400)
						 	  .animate({"opacity":"1"},500);

}

function fade() {
	$('html').css({"overflow":"auto"});
	$('#black-bg').animate({"opacity":"0"},500);
	$('#black-front').animate({"opacity":"0"},500);
	var t=setTimeout("back()",600);
}

function back() {
	
	$('#black-bg').css({"z-index":"-1"});
	$('#black-front').css({"z-index":"-1"});

}

function video(mp4, ogv, description){
	black();
	var destination = document.getElementById("video");
	var edit = document.getElementById("vid_id");
	var html = '<video id="project-vid" height="80%" controls autoplay><source src=\'http://maculafilms.com/'+mp4+'.mp4\' type=\'video/mp4\' /><source src=\'http://maculafilms.com/'+ogv+'.ogv\' type=\'video/ogg; codecs="theora, vorbis"\' /><object id="flowplayer" data="flowplayer-3.2.2.swf" type="application/x-shockwave-flash" width="100%"  ><param name="movie" value="flowplayer-3.2.2.swf"><param name="allowfullscreen" value="true"><param name="flashvars" value="config={\'clip\':{\'url\':\''+mp4+'.mp4\',\'autoPlay\':false}}"></object> </video><br />';
	destination.innerHTML=html;
	
}

$("#up").live('click', function() {
	
	$('#panel').animate({"top":"-460px"}, {complete: function() {
		
	    }
	});
	
});

$("#down").live('click', function() {
	
	$('#panel').animate({"top":"0px"},400);
	
});

$(".right").live('click', function() {
	
	$('#thumb-slider #container').animate({"left":"-660px"},400);
	
});

$(".left").live('click', function() {
	
	$('#thumb-slider #container').animate({"left":"0px"},400);
	
});

$(".category").live('click', function() {
	if ($(this).hasClass("active")){
		$(this).removeClass("active");
	}
	else {
		$(this).addClass("active");
	}
	
	$(this).nextAll(".container").eq(0).toggle("slow");
	
});

$("#select").live('click', function() {
	$("#selects").slideToggle();
});

$(".vid").live('click', function() {
	
	var url = "work/";
	url = url + $(this).attr("title");
	var mp4 = $(this).attr("mp4");
	var ogv = $(this).attr("ogv");
	var vidId = $(this).attr("vid");
	var description = $(this).attr("description");
	video(mp4, ogv, description);
	window.history.pushState("object or string", "Title", url );
	
});

$("#work #black-bg").live('click', function() {
	controller.stop();
	var url = "/work";
	fade();
	window.history.pushState("object or string", "Title", url );
	var destination = document.getElementById("video");
	destination.innerHTML="";
	
});

$("#home #black-bg").live('click', function() {
	controller.stop();
	var url = "/home";
	fade();
	window.history.pushState("object or string", "Title", url );
	var destination = document.getElementById("video");
	destination.innerHTML="";
	
});


$("#home .close").live('click', function() {
	controller.stop();
	var url = "/home";
	fade();
	window.history.pushState("object or string", "Title", url );
	var destination = document.getElementById("video");
	destination.innerHTML="";
	
});

$("#work .close").live('click', function() {
	controller.stop();
	var url = "/work";
	fade();
	window.history.pushState("object or string", "Title", url );
	var destination = document.getElementById("video");
	destination.innerHTML="";
	
	
});



$("#login-container .close").live('click', function() {
	
	$(".black").fadeOut("fast");
	$("#login-box").fadeOut("fast");
	$("body").css({"overflow":"auto"});
	
});

$(".login").live('click', function() {
	
	$(".black").css({"opacity":"0.7"}).fadeIn("fast");
	$("#login-box").fadeIn("fast");
	$('html, body').animate({scrollTop: '0px'}, {
			duration: 400,
			complete: function(){
					$('html').css({"overflow":"hidden"});
			}
	});
	
	
});

$(".black").live('click', function() {
	
	$(".black").fadeOut("fast");
	$("#login-box").fadeOut("fast");
	$("body").css({"overflow":"auto"});
	
});


$(function() {
	
	$('.error').hide();
	
	$(".overlay").css({"opacity":"0.8"});
	$("#background").css({"opacity":"0.3"});
	
	$('select.styled').customStyle();
	
	$(".hover img").hover(function() {
	    $(this).attr("src", $(this).attr("src").split(".").join("_over."));
	  }, function() {
	    $(this).attr("src", $(this).attr("src").split("_over.").join("."));
	});
	
	$("a.work").mouseenter(function() {
			$(this).addClass("active");
	    $("#work-drop").fadeIn();
	});
	
	$("#work-drop").mouseleave(function() {
	    $(this).fadeOut();
			$("a.work").removeClass("active");
	});
	
	workDrop();
	
	$("a.vid").hover(function() {
	    $(this).find('.overlay').fadeIn();
															
	  }, function() {
	    $(this).find('.overlay').fadeOut();
	});
	
		// Array of images:
		var imageArray = ['images/1a_over.png', 'images/1b_over.png', 'images/2a_over.png', 'images/2b_over.png', 'images/3a_over.png', 'images/3b_over.png', 'images/reel_over.png'];

		// Add hidden element
		var hidden = $('body').append('<div id="img-cache" style="display:none/>').children('#img-cache');

		// Add images to hidden element.
		$.each(imageArray, function (i, val) {
		  $('<img/>').attr('src', val).appendTo(hidden);
		});
		
		$(".save").click(function() {
			// validate and process form
			// first hide any error messages
		  $('.error').hide();

			var type				= $("select#type").val();  
		  var company		 	= $("input#company").val();
		  var title				= $("input#title").val();
		  var description	= $("textarea#description").val();
		  var projId				= $("input#proj_id").val();

		  var redirect		= "http://maculafilms.com/work";

		  // if (type == "") {  
		  // 		  	$("label#category_error").show();  
		  // 		  	$("select#type").focus();  
		  // 		  	return false;  
		  // 			}
		  // 			if (title == "") {
		  // 		    $("label#title_error").show();
		  // 		    $("input#title").focus();
		  // 		    return false;
		  // 		  }

			var dataString = 'id='+ projId + '&company='+ company + '&title='+ title + '&description=' + description + '&type=' + type;
			//alert (dataString);return false;

			$.ajax({
		    type: "POST",
		    url: "includes/video-edit.php",
		    data: dataString,
		    success: function() {
		      $('#add-vid').html("<div id='message'></div>");
		      $('#message').html("<h2>Video added successfully!</h2>")
		      .append("<p>You will be redirected to your Work Page shortly.</p>")
		      .hide()
		      .fadeIn(1500, function() {
		        $('#message').append("<img id='checkmark' src='images/check.png' />");
						var t=setTimeout(window.location=redirect,7000);
		      });
		    }
		   });
		  return false;
		});	
		
		
		$(".saveblog").click(function() {
			// validate and process form
			// first hide any error messages
		  $('.error').hide();

			var title				= $("input#blog-title").val();  
		  var description	= $("textarea#blog-description").val();
			
			var vids = [];
			    $(':checkbox:checked').each(function(i){
			      vids[i] = $(this).val();
			    });
			
			var vids = vids.toString();

		  var redirect		= "http://maculafilms.com/home";

			// var dataString = 'title='+ title + '&description=' + description + '&videos=' + vids;
			
			var dataToSend = {'title' : title, 'description' : description, 'videos' : vids} ;
			
			//alert (dataString);return false;

			$.ajax({
				traditional: true,
		    type: "POST",
		    url: "includes/addblog.php",
		    data: dataToSend,
		    success: function() {
		      $('#add-blog').html("<div id='message'></div>");
		      $('#message').html("<h2>Video added successfully!</h2>")
		      .append("<p>You will be redirected to the homepage shortly.</p>")
		      .hide()
		      .fadeIn(1500, function() {
		        $('#message').append("<img id='checkmark' src='images/check.png' />");
						// var t=setTimeout(window.location=redirect,7000);
		      });
		    }
		   });
		  return false;
		});
		
		
				
				
});		


$(window).resize(function(){
	workDrop();
});

/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();

/* -------------------------- */
/* LOGIN */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function login() {
// Optional: Show a waiting message in the layer with ID ajax_response
document.getElementById('login_response').innerHTML = "Loading..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var usr = encodeURI(document.getElementById('usrLogin').value);
var psw = encodeURI(document.getElementById('pswLogin').value);
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'includes/login.php?usr='+usr+'&psw='+psw+'&nocache = '+nocache);
http.onreadystatechange = loginReply;
http.send(null);
}
function loginReply() {
if(http.readyState == 4){ 
var response = http.responseText;
if(response == 0){
// if login fails
document.getElementById('login_response').innerHTML = 'Login failed! Verify user and password';
// else if login is ok show a message: "Welcome + the user name".
} else {
document.getElementById('login_response').innerHTML = response;
eval(document.getElementById('sc1').innerHTML);

}
}
}

function editVid(vidId) {
// Optional: Show a waiting message in the layer with ID ajax_response
document.getElementById('black-front').innerHTML = "Loading..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var vid_id = vidId;
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'includes/edit-vid.php?id='+vid_id+'&nocache = '+nocache);
http.onreadystatechange = vidReply;
http.send(null);
}
function vidReply() {
if(http.readyState == 4){ 
var response = http.responseText;
if(response == 0){
// if login fails
document.getElementById('black-front').innerHTML = 'Login failed! Verify user and password';
// else if login is ok show a message: "Welcome + the user name".
} else {
document.getElementById('black-front').innerHTML = response;
// eval(document.getElementById('sc1').innerHTML);

}
}
}

function redirect(str)
{
	
	if (str=="usr"){	
		var redirect = "http://maculafilms.com/home";
	}
	
	if (str=="admin"){	
		var redirect = "http://maculafilms.com/admin";
	}
	if (str=="home"){	
		var redirect = "http://maculafilms.com/home";
	}
	$('#login-container').fadeOut();
	var t=setTimeout(url(redirect), 10000);
}

function url(str) 
{
	window.setTimeout(function() { window.location = str; }, 1000);
}

function vidSize(){
	var vid = document.getElementById("project-vid");
	vid.videoHeight; // returns the intrinsic height of the video
	vid.videoWidth; // returns the intrinsic width of the video
}

		
