//

$(document).ready(function() {
  
	$('#submenu').hide();

	original_heading = $('#menu_heading').text();
  //button rollover
	$("img.button").hover(
		function() {
		  this.src = this.src.replace("_up","_down");
		  $('#menu_heading').text($(this).attr('alt'));
		  $('.catmenu').hide();
		  if ($(this).attr('id')=='m_home') {
				$('#submenu').stop().animate({height:'0px'});
			}
			else {
			  $('#c_'+$(this).attr('id')).show();
			  $('#submenu').stop().animate({height:'200px'});
			}
		},
		function() {
		  this.src = this.src.replace("_down","_up");
		}
 	); // END button rollover 
	$("#menu_heading").hover(
		function() {
		  $('#menu_heading').text(original_heading);
		  $('.catmenu').hide();
		  if (original_cat_menu!='') {
			  $('#c_m_'+original_cat_menu).show();
			  $('#submenu').stop().animate({height:'200px'});
			}
		},
		function() {
		}
 	); 
	$('#menu').hover(function(){
		//$('#submenu').stop().animate({height:'200px'});
		},	function(){
		  $('.catmenu').hide();
			$('#menu_heading').text(original_heading);
			$('#submenu').stop().animate({height:'0px'});
		});

	
	// this is needed solely for <button> element
	$('button').hover(function(){
		//console.log($(this).find("img"));
		$(this).find("img").attr("src",$(this).find("img").attr("src").replace("_up","_down"));
	},function(){
		$(this).find("img").attr("src",$(this).find("img").attr("src").replace("_down","_up"));
	});

	// this fixes the special_tile height - to be equal to img height
	$('.special_tile').each(function(){
		//console.log($(this).find('img').height());
		var height = Math.round($(this).find('img').height()/180)*180-1; //sets height to nearest multiple of 180
		var width = Math.round($(this).find('img').width()/240)*240-1; //sets width to nearest multiple of 240
		//console.log(height);
		$(this).height(height);
		$(this).width(width);
		//console.log($(this).height());
	});
  
/* 
there seems to be a bug in Masonry
this is to make sure the bigger tile is always on even position		
*/		
	$(".twocol_tile").each(function(){
	
		var tiles_count = 0; 
	    //console.log("tiles no: "+Math.round($(this).width()*$(this).height()/(240*180)));
		$(this).prevAll('div').each (function () { tiles_count += Math.round($(this).width()*$(this).height()/(240*180)); });
		
		if (tiles_count%2) {
			//console.log("shifting...");
			$(this).insertAfter($(this).next());
		}

	});
	

  
  

	function set_bgimage() {
		//console.log($(window).width());
		if (typeof bgname=='undefined' || bgname=='') {
			bgname='mtb';
		}
	  if($(window).width()>1280) {
			if($(window).width()>1680) {
				$('body').css('background-image','url(\'images/backgrounds/'+bgname+'1920.jpg\')');
				//console.log("loaded 1920");
			} else {
				$('body').css('background-image','url(\'images/backgrounds/'+bgname+'1680.jpg\')');
				//console.log("loaded 1680");
			}	
		} else {
			$('body').css('background-image','url(\'images/backgrounds/'+bgname+'1280.jpg\')');
			//console.log("loaded 1280");
		}
	}
	
	set_bgimage();
	
	$(window).resize(function(){set_bgimage();});

 });