var twSpeed = 700;
var badie = false;

$.each(jQuery.browser, function(i, val) {
  var browsVersion = $.browser.version;
  if(i=="msie"){
	if(browsVersion < 9){
		//alert("msie");
		badie = true;
	}
  }
  //alert('You are using: '+i+' - '+val+' - '+browsVersion);
  //if(i=="mozilla" && jQuery.browser.version.substr(0,3)=="1.9")
  //   alert("Do stuff for firefox 3")
});




$(document).ready(function(){

function init_sifr_title(){
	//alert('sifr')
	/**/
	
	sIFR.replace(specialfont, {
	selector: '.big_block_left',
	css: '.sIFR-root { color: #ffffff; text-transform:lowercase; font-size: 20px; line-height: 10px; }'
	});

}

init_sifr_title();

function init_sifr(){
	//alert('sifr')
	/**/
	sIFR.replace(specialfont, {
	selector: 'ul.focus h2',
	css: '.sIFR-root { color: #ffffff; text-transform:lowercase; font-size: 20px; line-height: 10px;}'
	});
}

init_sifr();

function center_top_bottom(){
	if(!badie){
		var wrapperh = $('div#wrapper').height();
		var windowh = $(window).height();
		if(wrapperh < windowh){
			var k = ((windowh - wrapperh)/2)+2;
			$('div#wrapper').animate({marginTop: k}, 1100);
			//$(window).animate({scrollTop:0}, 'slow');
			$('html, body').animate({scrollTop:0}, 'slow');
		}
		else{
			$('div#wrapper').animate({marginTop: 100}, 1100);
		}
		//alert('windowh: '+windowh);
	}
	
}

center_top_bottom();

$(window).resize(function(){
 // alert("Stop it!");
	center_top_bottom();
});

//$('div#wrapper').resize(function(){
//  alert("Stop it!");
//});


function init_image_expander(){
	//$('div.img_full img').animate({ top: 0, left: 0}, 40);
	
	$('div.img_thumb img').unbind("click");
	$('div.img_thumb img').click(function(){
		//alert('click image: '+lastfocuswidth);
		var newwidth = lastfocuswidth + 360;
		//$('#wrapper').animate({width: newwidth}, 500);
		
		$(this).fadeOut('slow');
		$(this).parent().parent().siblings().find('div.img_thumb img').fadeIn(2000);
		//$(this).parent().parent().siblings().animate({height: 155}, 500); // sibling lis
		var h = $(this).parent().parent().find('div.img_full').height();
		//$(this).parent().parent().find('div.img_full').animate({ opacity: 100}, 400);
		$(this).parent().parent().animate({width: 395, height: h}, 1000, function(){ center_top_bottom(); });
		$(this).parent().parent().siblings().animate({width: 75, height: 155}, 900);
	});
}



function center_my_pics(){
	$('div.img_thumb img').load(function(){ //each(function(indexDl) {	

				//$(this).fadeOut('fast');
	
				var w = $(this).width();
				var h = $(this).height();
				//alert('w: '+w+' h: '+h);
				
				if(h>w){
					//alert('move up');
					var posval = (h - w)/2;
					var posvall = "-"+posval;
					$(this).css('position', 'relative');
					$(this).animate({
						top: posvall,
						left: 0
						}, 400);
		
				}
		
				if(w>h){
					//alert('move up');
					var posval = (w - h)/2;
					var posvall = "-"+posval;
					$(this).css('position', 'relative');
					$(this).animate({
						top: 0,
						left: posvall
						}, 400);
				}
				
				//$(this).fadeIn('fast');
				 init_image_expander();
			//});			
	});
}





		var whichfocus = $('.focus li');
		var whichhalfs = $('.half li');				    
		var whichquarter = $('.quarter li');				    
		var whichhidden = $('.hidden li');				    
		var whichhiddendeep = $('.hiddendeep li');				    
		var whichhiddenrealdeep = $('.hiddenrealdeep li');
		
		var lastfocuswidth = 0;


function animate_hidden_to_focus(whichhidden) {
		//alert('animate_hidden_to_focus');
		//var whichhidden = $('.hidden li');
			$('ul object').hide();
			
			var test = whichhidden.attr('rel')
			//alert('in function'+test);
			
			var countfocus = 0;
			$(whichhidden).each(function(indexDl) {
				//var thistest = $(this).height();
				//alert(thistest);	
				//$(this).show();
				countfocus++;
				var randNum = (Math.floor(Math.random() * 500))+twSpeed;
				var randMargin = (Math.floor(Math.random() * 100))+30;
				//alert(randNum);
				$(this).animate({height: "0", width: "0", marginRight: randMargin, marginTop: randMargin}, randNum)
				.animate({height: "155", width: "155", marginRight: "0", marginTop: "0"}, randNum, function(){ 			
					
																						  			
					center_my_pics();
					
					if(test){
					setTimeout(init_sifr, 1000);
					}
					//else{ alert('no test'); }
				});
				$(this).find('object').show();
				

			}); 
			
			// each
				/*$('#title').animate({marginTop: 100}, 900, function() {
						//$('#title').animate({marginTop: 1}, 600);								  
				}); */
				
			
			//alert(countfocus);
			var totalfocuswidth = countfocus*160;
			lastfocuswidth = totalfocuswidth;
			if(totalfocuswidth < 600){ totalfocuswidth = 600;}
			//alert(totalfocuswidth);
			$('#wrapper').animate({width: totalfocuswidth}, 1000, function() { 
				
				center_top_bottom(); 
			});

			
			$(whichhidden).parent().removeClass('hidden').addClass('focus');
			//$('ul.focus').find('h2').animate({opacity: "100"}, 5000);
			//center_my_pics();
			//center_my_pics();
}





function init_focus() {	



	$(".quarter li").unbind("click");
	$(".hidden li").unbind("click");
	$(".focus li").unbind("click");
	$(".focus li").click(function(){
							
		$('ul object').hide();		
		
		var myliid = $(this).attr('id');
		if(myliid == 'about_button'){					
			//alert('myliid: '+myliid);
			var h = $('div#about .big_block_right').height();
			$('div#about').animate({width: 495, height: h, marginLeft: 80}, 1000, function(){ center_top_bottom(); });
		}
		if(myliid == 'contact_button'){					
			//alert('myliid: '+myliid);
			var h = $('div#contact .big_block_right').height();
			$('div#contact').animate({width: 495, height: h, marginLeft: 160}, 1000, function(){ center_top_bottom(); });
		}
		var whichfocus = $('ul.focus li');
		var whichhalfs = $('ul.half li');				    
		var whichquarter = $('ul.quarter li');				    
		var whichhidden = $('ul.hidden li');	
		var whichhiddendeep = $('ul.hiddendeep li');				    
		var whichhiddenrealdeep = $('ul.hiddenrealdeep li');				    
		var thisrel = $(this).attr('rel');
		//alert('focus');
		var test = whichhidden.attr('rel')
		//alert(whichhidden);
		
		
		
		
		if(thisrel != 'photo'){
		
		
			$(this).animate({opacity: "0", height: "0"}, 100);
			//$(this).addClass('loading_things');
			//
			$(whichfocus).each(function(indexDl) {
				var randNum = (Math.floor(Math.random() * 500))+twSpeed;
				var randMargin = Math.floor(Math.random() * 50);
				$(this).animate({height: "75", width: "75", marginRight: randMargin, marginTop: randMargin/2}, randNum)
				.animate({marginRight: "0", marginTop: "0"}, randNum, function(){
												   
				});
				//$(this).find('h2').animate({opacity: "0"}, 50);
	
				//.parent().removeClass('focus').addClass('half');
			}); // each
			$(whichfocus).parent().removeClass('focus').addClass('half');
		}
		
		//alert(thisrel);
		if(thisrel == 'portfolio' || thisrel == 'porttype' || thisrel == 'houses'){
			
			$(whichhalfs).each(function(indexDl) {
				var randNum = (Math.floor(Math.random() * 500))+twSpeed;
				var randMargin = Math.floor(Math.random() * 50);
				$(this).animate({marginRight: randMargin, marginTop: randMargin}, randNum)
				.animate({height: "35", width: "35", marginRight: "0", marginTop: "0"}, randNum)
				.parent().removeClass('half').addClass('quarter');
			}); // each


			$(whichquarter).each(function(indexDl) {
				var randNum = (Math.floor(Math.random() * 500))+twSpeed;
				var randMargin = Math.floor(Math.random() * 50);
				$(this).animate({height: "20", width: "20", marginRight: randMargin, marginTop: randMargin}, randNum)
				.animate({height: "15", width: "15", marginRight: "0", marginTop: "0"}, randNum)
				.parent().removeClass('quarter').addClass('eighth');
			}); // each
			
			if(thisrel != 'porttype' && thisrel != 'houses'){
				animate_hidden_to_focus(whichhidden);
			}
			
			$(whichhiddendeep).each(function(indexDl) {
				$(this).parent().removeClass('hiddendeep').addClass('hidden');
			}); // each
	
			$(whichhiddenrealdeep).each(function(indexDl) {
				$(this).parent().removeClass('hiddenrealdeep').addClass('hiddendeep');
			}); // each
		}
		
		
		/* FOR HOUSES */
		if(thisrel == 'porttype'){
			//alert('porttype');
			var page_id = $(this).attr('page_id');
			$.ajax({
				type: "POST",
				url: "append_houses.php",
				data: "page_id="+page_id,
				success: function(msg){
					$('ul#houses_holder').html(msg);//
					//alert( "Data Saved: " + msg );
					//var test = whichhidden.attr('rel')
					//alert(whichhidden);
					whichhidden = $('ul#houses_holder li');
					animate_hidden_to_focus(whichhidden);
					init_focus();	
					init_half();

				}
			});

		}
		
		
		/* FOR PHOTOS */
		if(thisrel == 'houses'){
			//alert('photos');
			var heading = $(this).find('h2').attr('alt');
			//alert('heading: '+heading);
			$.ajax({
				type: "POST",
				url: "append_photos.php",
				data: "heading="+heading,
				success: function(msg){
					$('ul#photos_holder').html(msg);//
					//alert( "Data Saved: " + msg );
					//var test = whichhidden.attr('rel')
					//alert(whichhidden);
					whichhidden = $('ul#photos_holder li');
					animate_hidden_to_focus(whichhidden);
					init_focus();	
					init_half();
					center_my_pics();

				}
			});

		}

		
		init_half();
		init_focus();

	});
}

init_focus();

function init_half() {

	$(".half li").unbind("click");
	$(".half li").click(function(){
	
		$('ul object').hide();		
	
		$('div#about').animate({width: 0, height: 0, marginLeft: 320}, 500);
		$('div#contact').animate({width: 0, height: 0}, 500);
				    
		$('ul#photos_holder img').remove();
		var whichfocus = $('ul.focus li');
		//alert(focuswidth);
		var whichhalfs = $('ul.half li');				    
		var whicheighth = $('ul.eighth li');				    
		var whichquarters = $('ul.quarter li');	
		var whichhidden = $('ul.hidden li');	
		var whichhiddendeep = $('ul.hiddendeep li');	
		var thisrel = $(this).attr('rel');
		
		if(thisrel != 'photo'){
		
			var countfocus = 0;
			$(whichhalfs).each(function(indexDl) {
				countfocus++;
				var randNum = (Math.floor(Math.random() * 500))+twSpeed;
				var randMargin = Math.floor(Math.random() * 50);
				$(this).animate({height: "105", width: "105", marginRight: randMargin, marginTop: randMargin}, randNum)
				.animate({height: "155", 
					     width: "155", 
						marginRight: "0", 
						marginTop: "0", 
						opacity: "100"}, randNum, function(){ 
							$(this).find('object').show(); 
						});
				
				$(this).parent().removeClass('half').addClass('focus');
				
			}); // each
		
			var totalfocuswidth = countfocus*160;
			if(totalfocuswidth < lastfocuswidth) {
				$('#wrapper').animate({width: lastfocuswidth+10}, 500).animate({width: totalfocuswidth}, 700, function() { center_top_bottom(); });
			}
			else {
				$('#wrapper').animate({width: totalfocuswidth}, 500, function() { center_top_bottom(); });
			}
			$(whichhalfs).animate({opacity: "100"}, 500);
		}

		//alert(thisrel);
		if(thisrel == 'portfolio' || thisrel == 'porttype' || thisrel == 'houses' || thisrel == 'home'){

			if(whichfocus.length > 0) {
				
				$(whicheighth).each(function(indexDl) {
					var randNum = (Math.floor(Math.random() * 500))+twSpeed;
					var randMargin = Math.floor(Math.random() * 25);
					$(this).animate({height: "60", width: "60", marginRight: randMargin, marginTop: randMargin}, randNum)
					.animate({height: "35", width: "35", marginRight: "0", marginTop: "0"}, randNum)
					.parent().removeClass('eighth').addClass('quarter');
				}); // each
				
				$(whichquarters).each(function(indexDl) {
					var randNum = (Math.floor(Math.random() * 500))+twSpeed;
					var randMargin = Math.floor(Math.random() * 50);
					$(this).animate({height: "60", width: "60", marginRight: randMargin, marginTop: randMargin}, randNum)
					.animate({height: "75", width: "75", marginRight: "0", marginTop: "0"}, randNum)
					.parent().removeClass('quarter').addClass('half');
				}); // each
					
				$(whichfocus).each(function(indexDl) {
					var randNum = (Math.floor(Math.random() * 500))+twSpeed;
					var randMargin = Math.floor(Math.random() * 50);
					$(this).animate({height: "105", width: "105", marginRight: randMargin, marginTop: randMargin}, randNum)
					.animate({height: "0", width: "0", marginRight: "0", marginTop: "0"}, randNum)
					.parent().removeClass('focus').addClass('hidden');
				}); // each
				
				$(whichhidden).each(function(indexDl) {
					$(this).parent().removeClass('hidden').addClass('hiddendeep');
				}); // each
		
				$(whichhiddendeep).each(function(indexDl) {
					$(this).parent().removeClass('hiddendeep').addClass('hiddenrealdeep');
				}); // each

			}
		}
		
		init_focus();	
		init_half();
	});
}

	
	
});
