/* Author:

*/

function reloadImage(id) {
	
	var obj = document.getElementById(id);
   	var url = obj.src;
	var now = new Date();
	
	obj.src = url + '?' + now.getTime();
	/*var now = new Date();
    if (document.images) {
        document.images.name.src = src + '?' + now.getTime();
    }*/
   
}

//set thumnail size on results 
function getImageSize(el, width) {

	var nWidth, is_chrome, div, height = 450;
	
	is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome');
	
	//check if user is using google chrome
	/*if (is_chrome > -1) {
		div = 280; //temporary fix for large landscape image
	}else {
		div = 281; //temporary fix for large landscape image
	}*/
	
	//check if initial image has loaded smaller than large version of 450 height (happens mostly when loading images for the first time)
	if(width > 363) {	
		//nWidth = 345;
		//height = 450;
		//div = 345;
		$(el).attr('id','1: - ' + width);
		//$(el).attr('height','211').removeAttr('width');
		//return;
	}
	// otherwise calculate from original large 450 height
	else {
		//nWidth = 300;
		//height = 211;
		//div = 162;
		$(el).attr('id','2: - ' + width);
	}
	
	/*/*if(height > nWidth) {
		$(el).attr('width','162').removeAttr('height');
	}else {
		$(el).attr('height','211').removeAttr('width');
	}*/

	if (width <= 363 && width < height) {//narrow portrait
		//alert("1");
		$(el).attr('width','162').removeAttr('height');
		//return;
	} else if (width > height) {//landscape
		//alert("2");
		$(el).attr('height','201').removeAttr('width');
		//return;
	} else if (width > height) {//landscape
		//alert("2");
		$(el).attr('height','201').removeAttr('width');
		//return;
	} else if (width <= height || width > 363) { //square
		//alert("3");
		$(el).attr('height','201').removeAttr('width');
		//return;
	} else {
		//alert("4");
		$(el).attr('height','201').removeAttr('width');
		//return;
	}
}

//hide submit button on online form to prevent more than one submission 
function ValidatePage() {

if (typeof (Page_ClientValidate) == 'function') {
	   Page_ClientValidate();
   }

   if (Page_IsValid) {
	   // do something
	   $("#submit").css("display", "none");
   }
   else {
	   // do something else
	   event.preventDefault();
   }
}


jQuery(document).ready(function ($) {
	
	
	//results page thumbnail load
	$('#t1 ul li').each(function(index,el){ 
		var img = new Image();
		var url = $(this).find('a.mdl-link').attr('rel');
		var width;
	
		$(this).find('a.mdl-link').html(img);
		
		$(img).load(function () {
				//width = $(this).width();
				//$(this).hide();
				$(this).attr('id','img' + index);
				$(el).removeClass('loading');
				$(el).find('div.loading-gif').hide();
				$(el).find('div.imgcontainer').fadeIn();
				//  ie 7 bug: - remove width and height attributes when image loads
				if (navigator.appVersion.indexOf('MSIE 7.') != -1) {
					$(this).removeAttr('width').removeAttr('height');
				}
		}).error(function () {
			//alert('Error loading image #' + index);
		}).attr('src', url);
		
		var intervalId = setInterval(
		  function()
		  {
			if(img.complete)
			{
			  if(navigator.appName == 'Microsoft Internet Explorer') {
					//width = $(img).width();
					clearInterval(intervalId);
					//alert('IE ' + $(img).width());
					width = $(img).width();
				}else{
					//width = img.width;
					clearInterval(intervalId);
					width = img.width;
				}
				//calculate image size to fit .model-img container
				getImageSize($(img), width);
				
				//$(img).attr('title','complete');
				
			}else {
				//$(img).attr('height','211').removeAttr('width');
				//document.images['img' + index].onload = function(){ this.complete= true};
				//$(img).complete = true;
				
				/*if(navigator.appName == 'Microsoft Internet Explorer') {
					//width = $(img).width();
					//clearInterval(intervalId);
					//alert('IE ' + $(img).width());
					width = $(img).width();
				}else{
					//width = img.width;
					//clearInterval(intervalId);
					width = img.width;
				}
				//calculate image size to fit .model-img container
				getImageSize($(img), width);*/
				
				//$(img).attr('title','not');
			}

			
		  },1);
		
	});
	
});



