var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

var ok_close = true;
/*
* Hiding the shadowbox called when the background is clicked
*/
function hideBox(){
	if(ok_close)
		Shadowbox.close();
}

function cancelHide(){
	ok_close = false;
	//we delay a little bit and say it is ok to close.
	//this allows the hideBox event to fire, but not run
	setTimeout(function(){
		ok_close = true;
	},50);
}

$(function(){
	var options = {
        resizeLgImages:true,
        assetURL:"/peary/",
        skin: {
        	main:'<div id="shadowbox_overlay"></div>' +
                        '<div id="shadowbox_container">' +                		
                            '<div id="shadowbox" onclick="hideBox()">' +
								'<div id="shadowbox_title" style="margin-top:10px;text-align:center;" onclick="cancelHide()">' +
                                    	'<div id="shadowbox_title_inner"></div>' +
                                	'</div>' + 
                                '<div id="shadowbox_body" onclick="cancelHide()">' +
                                    '<div id="shadowbox_body_inner"></div>' +
                                    '<div id="shadowbox_loading"></div>' +
                                '</div>' +
                                '<div id="shadowbox_toolbar" style="margin:auto;width:550px;" onclick="cancelHide()">' +
                                    '<div id="shadowbox_toolbar_inner"></div>' +
                                '</div>' +
                            '</div>' +
                        '</div>',

            loading:    '<img src="{0}" alt="{1}" />' +
                        '<span><a href="javascript:Shadowbox.close();">{2}</a></span>',

            counter:    '<div id="shadowbox_counter">{0}</div>',

            close:      '<div id="shadowbox_nav_close">' +
                            '<a href="javascript:Shadowbox.close();">{0}</a>' +
                        '</div>',

            next:       '<div id="shadowbox_nav_next">' +
                            '<a href="javascript:Shadowbox.next();">{0}</a>' +
                        '</div>',

            prev:       '<div id="shadowbox_nav_previous">' +
                            '<a href="javascript:Shadowbox.previous();">{0}</a>' +
                        '</div>'
        }
    };
    
	Shadowbox.init(options);
	
	headline_count = $("div.headline").size();
  	$("div.headline:eq("+current_headline+")").css('top', '5px');
  	headline_interval = setInterval(headline_rotate,5000);
  	$('#scrollup').hover(function(){
		clearInterval(headline_interval);
  	},function() {
		headline_interval = setInterval(headline_rotate,5000);
		headline_rotate();
  	});
});


function headline_rotate() {
  	current_headline = (old_headline + 1) % headline_count;
  	$("div.headline:eq(" + old_headline + ")").animate({top: -205},"slow", function() {
		$(this).css('top', '210px');
	});
  	$("div.headline:eq(" + current_headline + ")").animate({top: 5},"slow");
  	old_headline = current_headline;
}

var shownAlbum = null;

function showDeleteAlbum(album){
	$("#" + shownAlbum + "popDel").css({"display":"none"});
	$("#" + shownAlbum + "popView").css({"display":"none"});
	$("#" + shownAlbum + "popEdit").css({"display":"none"});	
	
	$("#" + album + "popDel").slideDown();
	shownAlbum = album;
	
}

function hideDeleteAlbum(album){

	$("#" + album + "popDel").slideUp();
	shownAlbum = null; //nothing is now shown
}

function showAlbum(albumName, realName){
	$("#" + shownAlbum + "popView").css({"display":"none"});
	$("#" + shownAlbum + "popDel").css({"display":"none"});
	$("#" + shownAlbum + "popEdit").css({"display":"none"});
		
	//if(shownAlbum != albumName){
		$("#" + albumName + "popView").css({"display":"block"});	
	
		//make ajax request to get thumbs of all the pics
		 $.ajax({
		   type: "POST",
		   url: "/peary/pages/function/albumImages.php",
		   data: "album=" + albumName,
		   success: function(msg){
		   		
		   		//$("#album").val(realName);
		   		$("#upload").remove();
		   		$("#photoList").remove();
		   		
				var resp = eval(msg);
				var uploadForm = "<div id='upload' style='float:left;width:400px;margin-top:15px;'>Upload: <form><input id='fileToUpload' name='fileToUpload' type='file' class='submit' style='margin-bottom:2px;'><input type='hidden' id='album' name='album' value='"+realName+"'  class='submit'><button onclick='return ajaxFileUpload();' style='border:1px solid #AAA;background:F0F0F0;font-size:12px;padding:2px;margin-left:3px;'>Upload</button> <a href='#' onclick='hideAlbum(\""+albumName+"\");return false;'>Cancel</a></form></div>";
				
				if(resp.length == 0){
					
					var out = "<div id='photoList' class='ajaxPhotos'><div id='noimg' style='float:left;width:450px;padding-top:25px;' align='center'>No images in this album.</div></div>";
					//show the upload now
					out += uploadForm;
					
					$("#" + albumName + "popView").html(out);
				}else{
					//theres some images
					var img = "<div id='photoList' class='ajaxPhotos'>";
					var i=0;
					while(i < resp.length){
						
						img += "<div class='pic' align='center'><a rel='shadowbox[" + realName + "]' title='" +realName+ "' href='/peary/upload_photo/" + realName + "/" + resp[i].image + "'>";
						img += "<img src='/peary/upload_photo/" + realName + "/thumb/" + resp[i].image + "'></a>";
						img += "<br>[<a href='#' onclick='deletePhoto(this, \""+ realName +"\", \""+ resp[i].image +"\");return false;'>Delete</a>]</div>";
						
						i++;
					}
					img += "</div>"//close photoList
					//show the upload now
					img += uploadForm;
					;
					//alert(img);
					$("#" + albumName + "popView").html(img);
					
					Shadowbox.setup();//call this to setup on all the images now
				}
		   }
		 });
		 shownAlbum = albumName;
	//}else{
	//	shownAlbum = null;
	//}
	
	
	
}

function showRenameAlbum(album){
	$("#" + shownAlbum + "popDel").css({"display":"none"});
	$("#" + shownAlbum + "popView").css({"display":"none"});
	$("#" + shownAlbum + "popEdit").css({"display":"none"});
	
	//if(shownAlbum != album){
		$("#"+album+"popEdit").slideDown();
		shownAlbum = album;
	//}else{
	//	shownAlbum = null;
	//}
	
}

function saveRename(albumName, idName){
	var newName = $("#"+idName+"newname").val();
	//alert("Saving: " + newName + " for old album: " + albumName);
	goTo("/peary/pages/function/renameAlbum.php?album=" + albumName + "&newname=" + newName);
}

function cancelRename(album){
	$("#" + album + "popEdit").css({"display":"none"});
}

function hideAlbum(albumName){
	$("#" + albumName + "popView").css({"display":"none"});
}

function deletePhoto(id, folder, photo){
	//the passed id is from the <a>, we need to delete the parent <div>
	//structured like: <div><a><img><br><a IDHERE><div>
	$(id).parent().remove();
	$.ajax({
		type: "POST",
		url: "/peary/pages/function/deletePhoto.php",
		data: "folder=" + folder + "&image="+photo,
		success: function(msg){
			
		}
	});
}

/**
* show the hidden LI associated with the argument
*/
function show(what){
	var block = $(what).parent().next();
	if(block.hasClass("hidden") == true){
		//block.removeClass("hidden");
		block.fadeIn().removeClass("hidden");
		$(what).parent().children(".toggle").html("-").css("marginRight","8px");
	}else{
		$(what).parent().children(".toggle").html("+").css("marginRight","5px");
		block.fadeOut().addClass("hidden");
	}
	return false;
}

function goTo(loc){
	this.location.href = loc;
}

/*
* light the row in the admin panel question list
*/
var formerColor = "";
function light(which){
	formerColor = $(which).css("background-color");
	$(which).css("background","#FFFFFF");
}

/*
* unlight the row in the admin panel question list
*/
function unlight(which){
	$(which).css("background",formerColor);
}

/*
* administration question list clicked quesiton
*/
function viewQuestion(qid){
	goTo("/admin/questions/"+qid);
}

/*
* administration delete a question given the qid
*/
function deleteQuestion(qid){
	$.ajax({
		type: "POST",
		url: "/peary/pages/function/deleteQuestion.php",
		data: "qid="+qid,
		success: function(msg){
			
		}
	});
	goTo("/peary/admin/questions");
}


/*
* Show a video
*/
function showVideo(which){
	var vid = $(which).parent().next(".video");
	if(vid.css("display") == "block"){
		vid.css("display","none");
	}else{
		vid.css("display","block");
	}
	
}

Array.prototype.contains = function(ele){
	var i=0;
	while(i < this.length){
		if(this[i] == ele){
			return true;
		}
		i++;
	}
	return false;
}