/* Author: kishan sanghani */

$(function() {
	
	    var theWindow        = $(window),
            $bg              = $(".bg"),
            aspectRatio      = $bg.width() / $bg.height();
			
		var	no 					 = $('.slider section').length ;			
		var	sectionWidth         = $('section').width() + 10 ;
		var	containerWidth       = sectionWidth * no ;
		
        function resizeBg() {
               
                 if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
				    $bg
				    	.removeClass()
				    	.addClass('bg bgheight');
				} else {
				    $bg
				    	.removeClass()
				    	.addClass('bg bgwidth');
				}
              
                            
        }

	 function filterPath(string) {
     return string
      .replace(/^\//,'')  
      .replace(/(index|default).[a-zA-Z]{3,4}$/,'')  
      .replace(/\/$/,'');
	}
	
	
		var i = 0;
		var p = 0;
		
		 $(".slider-container section").each( function (e) {
		 var secWidth = $(this).width();
		 //alert (secWidth);
         var nx = Math.floor(i) * 605;
		 var len= $(this).find(".property").length;
		 var contWidth = (235 * len) + 54 ;
		  $(this).css({'width': contWidth+'px'});
		  
			++i;
			$(this).css({
				//"position": "absolute",
				//"left": contWidth + "px",
				//"margin-right" : "5px"
			})
		});
		
		
		 $(".slider-container section").each(function (a) {
		
		 //alert(len);
		
		  $(this).find(".property").each( function (e) {
				var nx = Math.floor(e) * 235;
				++e;
				
				if (e==1) {
				$(this).css({
					"position": "absolute",
					"left": "54px",
					"margin-right" : "5px"
				})
				}else {
				
				$(this).css({
					"position": "absolute",
					"left": nx + 54 + "px",
					"margin-right" : "5px"
				})
				
				}
					
		});
		
		 
		
		})
					
			
		//$('.slider-container').css({ width : containerWidth + 'px'});
	
	
	function positionSlider () {
		var Window           = $(window),
		headerHeight 		 = $('header').height(),
		footerHeight 		 = $('.bottom-strip').height() + 70,
		sliderTop			 = Window.height() - ( headerHeight + 390 + footerHeight );
		bgTop                = (Window.height()) * 15 / 100 ;
		bgHeight			 = $('.bg').height();
		
		
						
				
	//$('.slider').css({'marginTop':sliderTop+'px'});

	if (Window.height() < 760 && Window.width() > 1200 ) {
	//$('.bg').css({'margin-top':'-'+bgTop+'px'});
	}
	else {
	//$('.bg').css({'margin-top':'-'+ -20 - bgTop+'px'});
	}
						
	} 
	
//	function pageload(hash) {
//	// hash doesn't contain the first # character.
//		if(hash && hash.charAt(0)!='!') {
//
//		$('a[href="#'+hash+'"]').addClass('active');
//		var position = $('#2011').position().left;
//		$('.slider-container').animate({'marginLeft': "-" + position +"px"},1000);
//
//		//window.location.hash = hash;
//
//		//$('.slider').css({'overflow':'hidden'});
//
//		 //$('#'+hash).html(position);
//		 //alert(position);
//		}
//
//		else {
//		$('a[href="#2011"]').addClass('active');
//		var position = $('#2011').position().left;
//		$('.slider-container').animate({'marginLeft': "-" + position +"px"},1000);
//
//		//alert(position);
//		}
//
//		//alert('hi');
//	}
	
	$.history.init(pageload);
	
	positionSlider ();
	
	// $('a[href=#2011-more]').fadeOut();
	
	$('.years a[href*=#]').click(function (event) {
    event.preventDefault();
	
	var _href = $(this).attr('href');
	
	if (_href == '#2011' || _href == '#2011-more') {
		$('a[href=#2011-more]').fadeIn();	
	}
	else {
		$('a[href=#2011-more]').fadeOut();	
	}
	
	if ( filterPath(location.pathname) == filterPath(this.pathname)
    && location.hostname == this.hostname
    && this.hash.replace(/#/,'') ) {
	  
	 		  
      var $targetId = $(this.hash), $targetAnchor = $('[id=' + this.hash.slice(1) +']');
      var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : true;
     
	 $(this).siblings('a').removeClass('active');
	 $(this).addClass('active');
	 
	   
	 var position = $target.position().left;
	 //alert(position);
	 	
	 //$(this.hash).html(position);
	/////////////////////////////////////////////// Function to retive the id on click and filter it :: ENDS
	$('.slider-container').stop().animate({'margin-left': "-" + position +"px"},1000);
	//window.location.hash = this.hash;
     return true;
    }
	
	})
	
	
	//$('.property').addClass('active');	
	
	
		
	$('.property').click(function(i){
			
				
			$('.property').addClass('disabled');
			$(this).removeClass('disabled');
			 var _desHeight = ( $(this).find('.description').height() ) + 150;
					
			if ($(this).hasClass('active') == false) {
		
			$(this).stop().css({'z-index':'','opacity':1}).animate({marginTop:'-380px'},1000);
			$(this).find('.description').stop().animate({top:'-' + _desHeight + 'px'},1000);
			$(this).find('.des').stop().animate({top:'0px'},1000);
			
			$(this).queue(function () { 
				
				$(this).dequeue();
			});
			
			$(this).find('.image').stop().animate({top:'150px','height':'300px'},500);
			$('.whiteBg').fadeIn().animate({'opacity':0.5},500);
			
				
			$('.disabled').stop().css({'z-index':'99'}).animate({marginTop:'0px','opacity':1},1000).queue(function () { 
			
				$(this).find('.image').stop().animate({top:'0px','height':'150px'},500);
				$(this).find('.description').stop().animate({top:'0px'},500);
				$(this).find('.des').stop().animate({top:'-100px'},500);
										
			});
			
				$('.property').removeClass('active');
				$(this).addClass('active');					
			
			} else {
			
					
				$(this).stop().css({'z-index':'99'}).animate({marginTop:'0px','opacity':1},1000).queue(function () { 
				
					$(this).find('.image').stop().animate({top:'0px','height':'150px'},500);
					$(this).find('.description').stop().animate({top:'0px'},500);
					$(this).find('.des').stop().animate({top:'-100px'},500);
							
				});
				
				$('.property').removeClass('disabled');
				$(this).addClass('disabled');
				
								
				$('.whiteBg').fadeOut().animate({'opacity':0},500);						
				$('.property').removeClass('active');
							
			
			}
			
			var windWidth = ($(window).width()) - 250 ;
			var currPos = $(this).offset().left;
			
				if ( currPos >= windWidth ) {
					
					$('.slider-container').stop().animate({'margin-left': "-=250px"},1000);
				} else if (currPos <= 30 ) {
					$('.slider-container').stop().animate({'margin-left': "+=250px"},1000);
				}
			
			
		});
		
			
		
		$('.whiteBg').click(function () {
	
		
		$('.property.active').stop().css({'z-index':'99'}).animate({marginTop:'0px','opacity':1},1000).queue(function () { 
		
			
			$(this).find('.image').stop().animate({top:'0px','height':'150px'},500);
			$(this).find('.description').stop().animate({top:'0px'},500);
			$(this).find('.des').stop().animate({top:'-100px'},500);
					
		});
		$('.whiteBg').fadeOut().animate({'opacity':0},500); 
		$('.property').removeClass('active');

	}); 
	

	
	var n = 0;
	$(".bilgi").mouseenter(function() {
	n = 0;
	}).mouseleave(function() {
	n = 1;
	});
	
	$('.bilgi').click(function () {		
		$(this).hide();
		$('#email-form').css({'display':'inline-block'});
		
	})
	
	$('#email-form #email').focus(function () {
		if(this.value.trim()=='E-posta adresinizi giriniz')this.value='';
	})
	
	$('#email-form #email').blur(function () {
		if(this.value=='')this.value='E-posta adresinizi giriniz'
	})
	
	
	
	$(document).click(function(event) {
    if ( !$(event.target).is('#email-form , .bilgi , #email , #search-button')) {
		$('.bilgi').show();
         $("#email-form").hide();
    }
});
	
	
	if ( $('.bx-wrapper').length !=0 ) {
	  // $('.bx-wrapper').vAlign();
	 }
	 
		
		
	
	 // $('.main-content article').articleAlign();
	 
	 if ($('.main-content article hgroup').siblings('p').length !=0) {
		$('.main-content article hgroup').next('p').addClass('first-para')
	 }
	 
	 if ($('.main-content article hgroup').siblings('label').length !=0) {
		$('.main-content article hgroup').next('label').addClass('first-para')
	 }
	 
	 /* function positionSidebar () {
		  var mainSliderPosition = parseInt($('.bx-wrapper').css('margin-top'));
		  var positionSidebar = mainSliderPosition+=155;
		  $('.sidebar').css('top', mainSliderPosition); 
		}	 

	positionSidebar () */ 
	
$(window).resize(function() {
positionSlider ();
resizeBg();

if ( $('.bx-wrapper').length !=0 ) {
//	$('.bx-wrapper').vAlign();
}

 // $('.main-content article').articleAlign();


if ( $('.bx-wrapper').length ==0 ) { 

	

}


}).trigger("resize");	
	
})


