/**
 * @author katewykes
 */
//timeout
var t, counter, object;

$(document).ready(function(){
	/*hide on click*/
	$('#oddis').hover(function() {
		if ($('#oddis ul.subnav').css('display') == 'none') {
			$('#oddis ul').slideDown(200); //:not(:animated) after ul (to fix bounce effect)
		}
	}, function() {
		$('#oddis ul').slideUp(10);
	});
	$("#searchform form input").click(function(){
		$(this).attr("value", "");
	});
	$("#searchform form input").blur(function(){
		$(this).attr("value", "Search");
	});
 	//if mask is clicked  
    $('#mask').click(function () {
		$(this).hide();
		$('.window').hide();  
	}); 
	//if close button is clicked
	$('div#popup_close').click(function (e) {
		//Cancel the link behavior
		$('#mask').hide();
		$('.dialogpopupwindow').hide();
	});	

	//look for a div identified by the preload images id and 
	//PROJECT THUMBS
	var preLoadDiv = document.getElementById("preloadimages")
	if(preLoadDiv){
		var successload = false;
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		//Set heigth and width to mask to fill up the whole screen
		$("#mask").css({'width':maskWidth,'height':maskHeight});
		$('#mask').hide();

		// create image object
     	imageObj = new Image();
		object = $("div.darkgrey_bk img.main");
		imagehtml = "<img id=\"" + $(object).attr("id") +  "\" src=\"" + $(object).attr("src").replace("_ppthumb", "") + "\">";
		$("#mainimage").html(imagehtml);
		$("#mainimage img").hide();
		if (object) {
			checkload(0);
		}
		$("div.darkgrey_bk img").each(function(i) {
			imageObj.src = $(this).attr("src").replace("_ppthumb", "");
		});	
		/*FROM WORK THUMBS*/
		$("div.darkgrey_bk img").click(function(e){
			e.preventDefault();  
			//**new function called for video
			successload = callEmbed($(this).attr("id"));
			if (!successload) {
				imagehtml = "<img src=\"" + $(this).attr("src").replace("_ppthumb", "") + "\">";
				$("#mainimage").html(imagehtml); //put html for new image in mainimage div
				object = $("#mainimage img");
				$("#mainimage img").hide();
				//load and reload
				checkfromthumb(0);
			}
		});
		
	}		
});

/*
 * Project page - called if there is an embed associated
imageid in querystring
 */
function callEmbed(imageid){
	var successload = false;
	$('#ajaxcontent').hide();
	//call external file images/dialogpopup
	$.ajax({
 		url: "/images/dialogpopup",
 		data: ({id : imageid}),
		async: false,
		success: function(html){
			successload = fadeincontent(html); //this is the one that checks for object.width
		}
	})
	return successload;
}

/*(#mainimage)*/
//check mainimage
function checkfromthumb(counter){
	if(object[0].complete){
		$("#mainimage img").fadeIn("fast");
	}else {
		if (counter < 5) {
			counter += 1;
			t = setTimeout("checkfromthumb(" + counter + ")", 1000);
		}
	}

}

/*****SUPPORTING FUNCTIONS*******/
function checkload(counter){
	try {
		if ((object[0].complete)&&(counter>1)) {
			$("#mainimage img").fadeIn("fast");	
			//**
			callEmbed($("#mainimage img").attr("id")); 
		}
		else {
			if (counter < 5) {
				counter += 1;
				t = setTimeout("checkload(" + counter + ")", 2000);
			}
		}
	}catch(err){
	}	
}
/*FadeIn content and position*/
/*doesn't load in successfully in order to read the image on first go*/
function fadeincontent(html){
	var bVideo = false;
	$('#ajaxcontent').html(html);
	object = $('#dialogpopup').find("object");
	if (object.attr("width")) {
		$('#mask').fadeIn("slow");
		resizeDiv();
		bVideo = true;
	}else{
		//remove large image popup
		$('#mask').fadeOut("fast");
		/*object = $('#dialogpopup .content').find("img");
		if(!object){
			$('#mask').fadeOut("slow");
		}else{
			checkloadResize(0);			
		}*/
	}
	return bVideo;
}
/*****only needed for images*******/
function checkloadResize(counter){
	try {
		if (object[0].complete) {
			resizeDiv();
		}
		else {
			if (counter < 5) {
				counter += 1;
				t = setTimeout("checkloadResize(" + counter + ")", 1000);
			}
		}
	}catch(err){
		$('#mask').fadeOut("slow");
	}	
}

function isScrolledIntoView(elem) {
	var docViewTop = $(window).scrollTop();
	var docViewBottom = docViewTop + $(window).height();
	
	var elemTop = $(elem).offset().top;
	var elemBottom = elemTop + $(elem).height();
	
	return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}

function ScrollIntoView(elem, objwidth, objheight) {
	var docViewTop = $(window).scrollTop() + 20;
	var docViewBottom = docViewTop + $(window).height();
	var docWidth = ($(window).width() - objwidth) / 2;

	$('#dialogpopup').css('top', docViewTop + 'px');  //-$('#dialogpopup').height()/2
	$('#dialogpopup').css('left', docWidth + 'px');  //-$('#dialogpopup').height()/2
	return true;	
}
function resizeDiv(){
	/*get object width and height*/
	$('#dialogpopup').fadeIn("slow");
	objWidth = parseInt(object.attr("width")) + 20;
	objHeight = parseInt(object.attr("height")) + 50;
	$('#dialogpopup').css('width', objWidth + "px");
	$('#dialogpopup').css('height', objHeight + "px");
	$("#dialogpopup").animate({ 
		width: objWidth + 'px',
		height: objHeight + 'px'
    }, 500 );
	//Get the window height and width
	ScrollIntoView("#ajaxcontent", objWidth, objHeight);
	$('#ajaxcontent').fadeIn("slow");
	$('#loading').fadeOut("slow");
}
