// JavaScript Document

function initPage() {
	
}

$(document).ready(function() {
	
	function megaHoverOver(){
		$(this).find(".subnav").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {
					if (!$(this).parent().is('li'))
						rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".subnav").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".subnav").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".subnav").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
 
 
	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
 
	$("ul#topnav li .subnav").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);
 
});

function subHover(id) { 
	var img=$('#img'+id).attr('src');
	var end=img.lastIndexOf('.');
	var newImg=img.substring(0,end)+'_hover'+img.substring(end);
	$('#img'+id).attr({src: newImg});
}

function subUnHover(id) {
	var img=$('#img'+id).attr('src');
	var end=img.lastIndexOf('.');
	var newImg=img.substring(0,end-6)+img.substring(end);
	$('#img'+id).attr({src: newImg});
}

function showHideClass(classid) {
	if ($("."+classid).css('display')=='none')
		$("."+classid).show();	
	else
		$("."+classid).hide();	
}


function winSize() {
	var myWidth=0, myHeight=0;
  	if(typeof(window.innerWidth)=='number') { //Non-IE
    	myWidth=window.innerWidth;
    	myHeight=window.innerHeight;
  	} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { //IE 6+ in 'standards compliant mode'
  		myWidth=document.documentElement.clientWidth;
    	myHeight=document.documentElement.clientHeight;
  	} else if(document.body && ( document.body.clientWidth || document.body.clientHeight)) { //IE 4 compatible
  		myWidth=document.body.clientWidth;
    	myHeight=document.body.clientHeight;
  	}
	return new Array(myWidth,myHeight);
}


function showAll() {
	$('#hider').hide();	
	$('#searchdiv').remove();
}

function hideAllWin() { 
	if ($('#hider').length) $('#hider').show();
	else $('body').append('<div id="hider" style="position:absolute; top:0; left:0; width:100%; height:100%; background-image:url(images/hidebg.gif); z-index:199;"'+
					 ' onclick="javascript:showAll();"> </div>');
	
}

function searchHorse() { 
	hideAllWin(); 
	var winsize=new Array();
	var winsize=winSize(); 
	var left=(winsize[0]-400)/2;
	var tops=(winsize[1]-300)/2; 
	$.get('ajaxfiles/searchhorse.php',{top: tops, left: left },function(data){
		 $('body').append(data);
		 $('#searchdiv').css('left',left);
		 $('#searchdiv').css('top',tops);
		 $('#namn').focus();
		});
}

function updateRownumber(page) {
	var rows=$('#nbr').val();
	window.location=page+'&rows='+rows;
}

function updateChart(chartid,charttype,id,sid) {
	var chart = $("#"+chartid+"_img");
	var date = $("#"+chartid+"_date");
	chart.hide('fast').attr('src','images/paper_loader.gif').show('fast');
	$.getJSON('ajaxfiles/updatechart.php',{chart:charttype,id:id,sid:sid},function(data) {
		if (data.res==1) {
			chart.hide('fast').attr('src',data.src).show('fast');
			date.empty().append(data.date);
		} else {
			chart.hide();
			alert('Uppdateringen misslyckades! \l\n'+data.errmsg);
		}
	});
	return false;	
}

function getRaceMenu(mode) { 
	//$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; charset=utf-8"});
	$('.subnav .loading').append('&nbsp;&nbsp;&nbsp;<img src="images/menuwait.gif" alt="Wait, loading" />');
	$("#morelist").empty();
	$("#alphabeticlist div").removeClass('active');
	if (mode==1) {
		$.getJSON('ajaxfiles/race_alphabetic.php',{mode:mode},function(data) {
			$("#alphabeticlist div").addClass('inactive').unbind('click');
			$('.subnav .row li a').css('font-weight','normal');
			$("#raceitem").css('font-weight','bold');
			for (i=0;i<data.length;i++) {
				$("#"+data[i].letter).removeClass('inactive').bind('click', function() {getRaceMenu($(this).attr('id'))});
			}
			$('.subnav .loading').empty();
		});
	} else { 
		$.get('ajaxfiles/race_alphabetic.php',{mode:mode},function(data) {
			$("#"+mode).addClass('active'); 
			$("#morelist").append(data);
			$('.subnav .loading').empty();
		});
	}
}

function getMarefamMenu(mode) { 
	//$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; charset=utf-8"});
	$('.subnav .loading').append('&nbsp;&nbsp;&nbsp;<img src="images/menuwait.gif" alt="Wait, loading" />');
	$("#morelist").empty();
	$("#alphabeticlist div").removeClass('active'); 
	if (mode==1) {
		$.getJSON('ajaxfiles/marefam_alphabetic.php',{mode:mode},function(data) {
			$("#alphabeticlist div").addClass('inactive').unbind('click');
			$('.subnav .row li a').css('font-weight','normal');
			$("#marefamitem").css('font-weight','bold');
			for (i=0;i<data.length;i++) {
				$("#"+data[i].letter).removeClass('inactive').bind('click', function() {getMarefamMenu($(this).attr('id'))});
			}
			$('.subnav .loading').empty();
		});
	} else { 
		$.get('ajaxfiles/marefam_alphabetic.php',{mode:mode},function(data) {
			$("#"+mode).addClass('active'); 
			$("#morelist").append(data);
			$('.subnav .loading').empty();
		});
	}
}


function pageHasScrollbar() {
  return ($('body').height() > $(window).height());
}
/*
function elementEndInBrowserView(e) {
  $(e).waypoint( function() { return true, down}, { offset: 'bottom_in_view' } );
  return false;
}
*/

//PAGESPECIFIC FUNCTIONS
var SPECIAL = new Object();

SPECIAL.loadMareFam = function(msid) {
	if ($('div .loader').html().trim() == '') {
		var last = $(".marefam:last").attr("id");
		last = last.substring(1);
		$('div .loader').html('<img src="images/paper_loader.gif" alt="V&auml;nta..."> H&auml;mtar fler stofamiljer...');
		$.get('ajaxfiles/loadMarefamSystList.php?start='+last+'&limit=26&system='+msid+'&ajax=1', function(data) {
		  if (data!='')
			$(".marefam:last").after(data);
		  else
			$(window).unbind('scroll');
		  $('div .loader').empty();
		  if (data=='')
			return false;
		});
		return false;
	}
	return true;	
}
