$(document).ready(function(){	
	
	Cufon.replace('#menu a', {
		fontFamily: 'Trajan Pro',
		separate: 'none',	
		textShadow: '1px 1px rgba(255, 255, 255, 1)',	
		color: '#002d62',
		//color: '#333',
		hover: false
	});				
		
	Cufon.replace('#menu .active a', {
		fontFamily: 'Trajan Pro',
		separate: 'none',			
		color: '#fff',
		hover: false
	});					
	
	Cufon.replace('.title, .title-2, .title-3, .title-4, .lp, .zobacz-wiecej', {
		fontFamily: 'Trajan Pro',
		separate: 'words',		
		color: '#002d62',
		//color: '#333',
		hover: true
	});				
	
	Cufon.replace('.bip-2', {
		fontFamily: 'Trajan Pro',
		separate: 'none',				
		hover: true
	});				
	
	Cufon.replace('li.separator', {
		fontFamily: 'Trajan Pro',
		textShadow: '1px 1px rgba(255, 255, 255, 1)',	
		separate: 'none',				
		hover: false
	});		
	
	Cufon.replace('#newsletter_title', {
		fontFamily: 'Trajan Pro',
		separate: 'none',		
		//color: '#002d62',
        color: '#333',
		hover: true
	});

		

	$(".blurinput").focus(function(){
		$(this).css('background-color', '#ffffff');
	});
		$(".blurinput").blur(function(){
		$(this).css('background-color', '#abc3dd');		
	});	
	

	height = $('#sitemap').height();
	$('#sitemap ul li').height(height);
	$('#sitemap ul li ul li').height('auto');
	$('#left-menu .ullevel-3').children(':last-child').addClass('last');	
	
	//nieruchomosci
	var max = 0;	
	$('.estate-box-1').each(function(){
		if (max < $(this).height()) max = $(this).height();
	})
	$('.estate-box-1').each(function(){		
		$(this).children(':first-child').css('height',(max-10)+'px');
	})	
	
	//dodawanie klasy przy ul.digits
	di = 1;
	$('ul.digits li').each(function(){
		temp = $(this).html();
		temp = '<div class="left">'+di+'</div><div class="right">'+temp+'</div><div class="clear"></div>';
		$(this).html(temp);
		di++;
	})
	
	
	// osbługa zakładki praca
	$('.pane-1').each(function(){
		$(this).children(':first-child').addClass('panes');
		$(this).children(':first-child').children().addClass('hidden');
		$(this).children(':first-child').children(':first-child').removeClass('hidden');
		ile = $(this).children(':first-child').children().length;
		tabs = '<ul class="tabs">';
		left = 586/(ile-1);		
		for(i=0;i<ile;i++) {
			if (i==0)
				tabs += '<li class="active"><a href="#">'+i+'</a></li>';
			else if (i == ile -1)
				tabs += '<li class="last"><a href="#">'+i+'</a></li>';
			else
				tabs += '<li style="left: '+ (left*i) +'px"><a href="#">'+i+'</a></li>';
		}	
		tabs += '</ul>';		
		$(this).children(':first-child').after('<p class="pages"><b>1</b>/'+ile+'</p>');
		$(this).children(':first-child').before(tabs);
	});
	
	$('.pane-1 .tabs a').click(function(){
		$(this).parent().parent().children().removeClass('active');
		$(this).parent().addClass('active');
		i = 1; a = 1;
		$(this).parent().parent().children('li').each(function(){
			if ($(this).attr('class') == 'active' ||  $(this).attr('class') == 'last active')			
				a = i;					
			i++;
		})
		i = 1;
		$(this).parent().parent().parent().children('.panes').children('li').addClass('hidden');
		$(this).parent().parent().parent().children('.panes').children('li').each(function(){
			if (i==a)
				$(this).removeClass('hidden');			
			i++;
		})
		$(this).parent().parent().parent().children('.pages').children(':first-child').html(a);
		return false;
	})
	
	//obsługa tabów np: cudzoziemcy	
	$('.tabs-2').children('li').hover(
		function(){
			$(this).css('text-decoration','underline');
		},
		function(){
			$(this).css('text-decoration','none');
		}
	)
	ii = 0;
	$('.tabs-2 li').each(function(){		
		if (ii == 0) { 
			$(this).addClass('active');			
			$(this).addClass('first');			
		}	
		ii++;
	})
	ii = 0;
	$('.panes-2 .pane-2').each(function(){		
		if (ii > 0) $(this).addClass('hidden');			
		ii++;
	})
	
	$('.tabs-2 li').click(function(){		
		$(this).parent().children('li').removeClass('active');
		$(this).addClass('active');
		
		i = 1; a = 1;
		$(this).parent().children('li').each(function(){
			if ($(this).attr('class') == 'active')			
				a = i;					
			i++;
		})		
		i = 1;
		$('.panes-2').children('.pane-2').addClass('hidden')
		$('.panes-2').children('.pane-2').each(function(){					
			if (i == a) $(this).removeClass('hidden');
			i++;
		})
	});
	$('.select_all').click(function(){
		var root = $(this);
		var list = $(root.attr('rel'));
		var type = root.attr('checked')?1:0;
		
		list.find('input[type=checkbox]').attr('checked',type);
	});
});

jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

$(function( $ ){	
	$('.scroll').jScrollPane(
		{
			verticalDragMinHeight: 74,
			verticalDragMaxHeight: 74			
		}
	);
	
	var time = 500;
	var ile = 0;
	var count = 0;
	if ($('.slider-gallery').length) {		
		var s_id = $('.slider-gallery').attr('id');
		var count = s_id.charAt(s_id.length-1);		
	} 	
	
	$('.slider-photos a.link-to-photo').each(function(s){ile++})	
	$('.slider-photo-mask').scrollTo('#slide0', time, {
		easing:'easeOutQuart'
	});	
	
	if (ile <= count) $('.slider-gallery a.link-gallery-right').addClass('hiddenimporant');			
	$('.slider-gallery a.link-gallery-right').click(function () {									
		next = 0;						    		
		n = $(this).attr('href');		
		active = n.replace('#slide','');
		$('.slider-photo-mask').scrollTo($(this).attr('href'), time, {
			easing:'easeOutQuart'
		});		
		if (parseInt(active) == ile-count)
			$(this).addClass('hiddenimporant');
		else {			
			$(this).attr('href','#slide'+ (parseInt(active) + 1));
		}	
		
		if (parseInt(active) > 0)
			$('.slider-gallery a.link-gallery-left').removeClass('hiddenimporant');		
		
		$('.slider-gallery a.link-gallery-left').attr('href','#slide'+ (parseInt(active)-1) );		
			
		return false;
	})
	$('.slider-gallery a.link-gallery-left').click(function () {				
		
		next = 0;					    					    								    
		n = $(this).attr('href');		
		active = n.replace('#slide','');	
		$('.slider-photo-mask').scrollTo($(this).attr('href'), time, {
			easing:'easeOutQuart' //use this easing equation for a funny effect
		});
		
		if (parseInt(active) == 0)
			$(this).addClass('hiddenimporant');
		else
			$(this).attr('href','#slide'+ (parseInt(active) - 1));
	
		if (parseInt(active) < ile - count)
			$('.slider-gallery a.link-gallery-right').removeClass('hiddenimporant');			
		$('.slider-gallery a.link-gallery-right').attr('href','#slide'+ (parseInt(active) + 1));
		
		return false;
	})
	
	//tabs in contact
	$('.tab-contact-content').addClass('hidden');
	$('.tab-contact-head').css('background-position','0 -50px');
	$('.tab-contact-first .tab-contact-content').removeClass('hidden');	 
	$('.tab-contact-first .tab-contact-head').addClass('active');
	$('.tab-contact-head').click(
		function(){
			$(this).next().toggleClass('hidden');
			$(this).toggleClass('active');
	})
	//initialize(52.220517, 21.003492, 15, 'map_canvas');
	
	//colorbox
	$("a[rel='lightbox']").colorbox();	
});

function initialize(latitude, longitude, zoom, id) {
	var myOptions = {
	  scaleControl: true,
	  center: new google.maps.LatLng(latitude, longitude),
	  zoom: zoom,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	var map = new google.maps.Map(document.getElementById(id),
	    myOptions);
	
	var marker = new google.maps.Marker({
	  map: map,
	  position: map.getCenter()
	});
	var infowindow = new google.maps.InfoWindow();        
	google.maps.event.addListener(marker, 'click', function() {
	    infowindow.open(map, marker);
	});
}

function changePhoto(element,product_id, photo_id)
{		
	$(element).parent().children().removeClass('active');
	$('.thumb_3 .loader').removeClass('hidden');
	$(element).addClass('active');
	$.ajax({
		type: "POST",
		url: domain+"sfxEstate/changePhoto",
		data: "product_id="+product_id+"&photo_id="+photo_id,
		success: function(msg){
			$('.thumb_3').html(msg)
		}
 	});
	return false;
}

function showLoaderL(id)
{		
	$('#'+id+' .loader').removeClass('hidden')
}
function hideLoaderL(id)
{
	$('#'+id+' .loader').addClass('hidden')
}

var validObject = {
		errorPlacement: function(error, element) {},

		errorElement: "span",
		highlight:	function(element, errorClass) {
			jQuery(element).css('border', '1px solid #e2000f');			
		},
		unhighlight: function(element, errorClass) {
			jQuery(element).css('border', '1px solid #07356C');
		},
		
		submitHandler: function(form) {			
                        var form = $(form).removeClass('saved');
			var url = form.attr('action');
			var formid = form.attr('id');
                        var link = $('.link_submit');
                        function responce()
                        {
                            if(!form.hasClass('saved'))
                                return;
                            var pars = form.serialize();
                            
                            jQuery('#'+formid+' .loader').removeClass('hidden');
                            jQuery('#'+formid+' .submit').addClass('hidden');
                              
                            jQuery.post(url, pars, function(data){
                            	$('#input_agree').parent().css('color','#aaa');		
                                    jQuery('#dialog').html(data);
                                    jQuery('#dialog').dialog();
                                    jQuery(form).each(function(){    			
                                                    this.reset();
                                            });		
                                            $(form).removeClass('saved');	
                                            jQuery('#'+formid+' .loader').addClass('hidden');
                                            jQuery('#'+formid+' .submit').removeClass('hidden');
                                            link.removeClass('cboxElement');
                            });
                        }
                        if(form.hasClass('onlyValid'))
                        {
                            form.addClass('saved');
                            responce();
                        }else{
                        link.colorbox({inline:true,open:true,padding:0,onComplete:function(){
                                $('.input_submit').unbind('click').click(function(){
                                	var agree = $('#input_agree');
                                	agree.unbind('change').change(function(){
                                		var c = $(this).attr('checked');
                                		if(c)
                                			$(this).parent().css('color','#aaa');
                                	});
									if(!agree.attr('checked'))
									{
										agree.parent().css({'color':'red'});
										$.colorbox.resize();
										return false;
									}
                                    form.addClass('saved');
                                    $.colorbox.close();
                                });
                        },onClosed:function(){
                            responce();
                        }});
                        }
			return false;
		},
		
		rules: {			
			"newsletter[email]": {
				required: true,								
				email: true
			},
                        "sfx_estate_emails[email]" :{
                            required: true,
                            email: true
                        }
		},
		messages: {
		}
	}

function validateNewsletterForm(){
	var contactform = jQuery('#newsletterForm').validate(validObject);	
	var contactform = jQuery('#newsletterForm2').validate(validObject);	
}


function strpos (haystack, needle, offset) {   
    var i = (haystack + '').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}
