//$ = jQuery.noConflict(); 
$(document).ready(function() {
	icube_slider.init();

	if ($('.agent-ask').length) {
		var inputFocus = function() {
				$(this).removeClass("idleField");
		        if (this.value == this.defaultValue){
		        	this.value = '';
		    	}
		        if(this.value != this.defaultValue){
			    	this.select();
		        }
		    };
		    
		var inputBlur = function() {
		        if (this.value == ''){
		        	$(this).addClass("idleField");
		        	this.value = (this.defaultValue ? this.defaultValue : '');
		    	}
		    };	
	    $('.agent-ask input[type="text"]').addClass("idleField");
		$('.agent-ask input[type="text"]').focus(inputFocus);
	    $('.agent-ask input[type="text"]').blur(inputBlur);   
	}
	
	if ($('#latest-offers').length > 0) {
		icube_catslider.init();
	}	
	if ($('#offer-search-toggle').length > 0) {
		$('#search').toggle();
		$('#offer-search-toggle').click(function() {
			$('#search').slideToggle();	
			return false;
		});
	}
	
	if ($('#contact-form-toggle').length > 0) {
		if ($('#contact-form-error').length < 1) { $('#f-contact').toggle(); }		
		
		$('#contact-form-toggle').click(function() {
			$('#f-contact').slideToggle();	
			return false;
		});
		
	}
		
	if ($('#show-photos').length > 0) {
		$('.current-label').fadeOut(5);
		$('#label-photos').fadeIn(500);
		$('#show-photos').addClass('active');
		
		var max_height = parseInt($('#label-photos').height());
		
		if (parseInt($('#label-googlemap').height()) > max_height) max_height = parseInt($('#label-googlemap').height());
		if (parseInt($('#label-calculator').height()) > max_height) max_height = parseInt($('#label-calculator').height());
		if (parseInt($('#label-movies').height()) > max_height) max_height = parseInt($('#label-movies').height());
		
		max_height += 56;
		$('#offer-left').css('height', max_height+'px');
		
		$('#show-photos').click(function() {
			$('.current-label').fadeOut(500);
			$('.show-button').removeClass('active');
			$('#label-photos').fadeIn(500);
			$('#show-photos').addClass('active');
			return false;
		});
		
		$('#show-googlemap').click(function() {
			$('.current-label').fadeOut(500);
			$('.show-button').removeClass('active');
			$('#label-googlemap').fadeIn(500);
			$('#show-googlemap').addClass('active');
			return false;
		});
		
		$('#show-calculator').click(function() {
			$('.current-label').fadeOut(500);
			$('.show-button').removeClass('active');
			$('#label-calculator').fadeIn(500);
			$('#show-calculator').addClass('active');
			return false;
		});
		
		$('#show-movies').click(function() {
			$('.current-label').fadeOut(500);
			$('.show-button').removeClass('active');
			$('#label-movies').fadeIn(500);
			$('#show-movies').addClass('active');
			return false;
		});
	}
	
//	$("a[rel='lytebox[all]']").colorbox();
	//$("a[rel='lytebox']").colorbox();
	$("#send-email-link").colorbox({width:"50%", inline:true, href:"#send-email-box"});
	
	if ($('#send-offer-email').length > 0) {
		$('#send-offer-email').submit(function() {
			$('#send-offer-loading').slideToggle();
			if (isEmail($('#send-offer-adress').val())) {
				$.ajax({
				   type: "POST",
				   url: "wyslij-oferte-nieruchomosci.html",
				   data: "id="+$('#send-offer-id').val()+"&email="+$('#send-offer-adress').val(),
				   cache: false,
				   success: function(htmlres){			   	  
				   	  if (htmlres == 'ok') {
				   	   	 $('#send-offer-error').hide();
				   	  	 $('#send-offer-error2').hide();
						 $('#send-offer-email').slideToggle();
				         $('#send-offer-success').slideToggle();
					  } else {
					  	$('#send-offer-error2').slideToggle();
					  }   
				   }
				 });
			} else {
				$('#send-offer-error').slideToggle();
			} 
			 
			return false;
		});
	}

	if ($('.clipboard').length > 0) {	
		$('.clipboard').click(function() {
			
			if ($('#'+this.id).hasClass('no-clip')) {
				return true;
			}

			var tmp = explode('_',this.id);
			offer_id = tmp[1];

			$.ajax({
				type: "POST",
				url: "do-schowka.html",
				data: "id="+offer_id,
				cache: false,
				success: function(htmlres){	
					var tmp = explode('#',htmlres);	   	  
					if (htmlres != '') {
				         $('#clipboard_'+tmp[0]).hide();
				         $('#addedclipboard_'+tmp[0]).show();
				         $('#clipboard-contents').html(tmp[1]);        
					} 
				}
			 });		
			 
			return false;
		});
	}						
});

function isEmail(email)
{
	 if (email.indexOf(' ')==-1 
	      && 0<email.indexOf('@')
	      && email.indexOf('@')+1 < email.length
	 ) return true;
	 return false;
}

var icube_slider = {
	elements_amount : 3, // ilość elementów
	current_element : 1,
	stoped : false,
	run : null, // interval
	speed_time : 4000,
	fade_speed : 800,
	timer_speed : 700,
	
	init: function() {	
		this.elements_amount = $('#slider-nav').children().size();
		
		this.show(1);
		
		if (this.elements_amount < 2) this.stoped = true;
		
		for (var i = 1; i <= this.elements_amount; i++)
		{
			$('#slider-nav-'+i).click(function() {
				var cid = explode('-', this.id);
				cid = cid[2];
				
				icube_slider.fadeAllOut(cid);
				if (cid != icube_slider.current_element) { // pokaz jeśli nie jest aktualnym
					icube_slider.show(parseInt(cid));
				} else {
					clearInterval(icube_slider.timer);
				}
				
				if (icube_slider.stoped == true && icube_slider.current_element == cid) { // drugi raz kliknięto na ten sam link, uruchom pokaz slajdów	
					icube_slider.stoped = false;
					icube_slider.run = setInterval('icube_slider.runSlider()', icube_slider.speed_time);
				} else {
					icube_slider.stoped = true;	
					clearInterval(icube_slider.run);
				}
				icube_slider.current_element = parseInt(cid);
				
				return false;
		    });
		}		
		this.run = setInterval('icube_slider.runSlider()', this.speed_time); 		
	}, 
	
	fadeAllOut: function(cid) {
		for (var i = 1; i <= this.elements_amount; i++)
			if (i != cid) this.hide(i); 
	},
	
	stop: function() {
		this.stoped = true;
		clearInterval(this.run);	
	},
	
	runslideshow: function() {
		this.stoped = false;
		this.run = setInterval('icube_slider.runSlider()', this.speed_time); 	
	},
	
	show: function(cid) {
		$('#slide_'+cid).fadeIn(this.fade_speed);
		
		if ($.browser.msie) {
			//$('#slider-'+cid+' *').fadeIn(this.fade_speed);
		}
			
		$('#slider-nav-'+cid).addClass('active');	
	},
	
	hide: function(cid) {
		$('#slide_'+cid).fadeOut(this.fade_speed);	
		if ($.browser.msie) {
		//	$('#slider-'+cid+' *').fadeOut(this.fade_speed);
		}		
		$('#slider-nav-'+cid).removeClass('active');
	},	
	
	runSlider: function() {
		if (this.stoped == true) {
			clearInterval(this.run);
			return;
		}
		
		this.hide(this.current_element);
		if (this.current_element == this.elements_amount) {
			this.show(1);
			this.current_element = 1; 
		} else {
			this.show(this.current_element + 1);
			this.current_element++; 
		}		
	}
}

var icube_catslider = {
	elements_amount : 3, // ilość elementów
	current_element : 1,
	stoped : false,
	run : null, // interval
	speed_time : 5000,
	fade_speed : 800,
	timer_speed : 700,
	
	init: function() {	
		this.elements_amount = $('#latest-cats').children().size();
		
		this.show(1);
		
		if (this.elements_amount < 2) this.stoped = true;
		
		for (var i = 1; i <= this.elements_amount; i++)
		{
			$('#slider-cat-'+i).click(function() {
				var cid = this.id.slice(-1);
				icube_catslider.fadeAllOut(cid);
				if (cid != icube_catslider.current_element) { // pokaz jeśli nie jest aktualnym
					icube_catslider.show(parseInt(cid));
				} else {
					clearInterval(icube_catslider.timer);
				}
				
				if (icube_catslider.stoped == true && icube_catslider.current_element == cid) { // drugi raz kliknięto na ten sam link, uruchom pokaz slajdów	
					icube_catslider.stoped = false;
					icube_catslider.run = setInterval('icube_catslider.runSlider()', icube_catslider.speed_time);
				} else {
					icube_catslider.stoped = true;	
					clearInterval(icube_catslider.run);
				}
				icube_catslider.current_element = parseInt(cid);
				
				return false;
		    });
		}		
		this.run = setInterval('icube_catslider.runSlider()', this.speed_time); 		
	}, 
	
	fadeAllOut: function(cid) {
		for (var i = 1; i <= this.elements_amount; i++)
			if (i != cid) this.hide(i); 
	},
	
	stop: function() {
		this.stoped = true;
		clearInterval(this.run);	
	},
	
	runslideshow: function() {
		this.stoped = false;
		this.run = setInterval('icube_catslider.runSlider()', this.speed_time); 	
	},
	
	show: function(cid) {
		$('#cat-set-'+cid).fadeIn(this.fade_speed);
		
		if ($.browser.msie) {
			//$('#slider-'+cid+' *').fadeIn(this.fade_speed);
		}
			
		$('#slider-cat-'+cid).addClass('active');	
	},
	
	hide: function(cid) {
		$('#cat-set-'+cid).fadeOut(this.fade_speed);	
		if ($.browser.msie) {
		//	$('#slider-'+cid+' *').fadeOut(this.fade_speed);
		}		
		$('#slider-cat-'+cid).removeClass('active');
	},	
	
	runSlider: function() {
		if (this.stoped == true) {
			clearInterval(this.run);
			return;
		}
		
		this.hide(this.current_element);
		if (this.current_element == this.elements_amount) {
			this.show(1);
			this.current_element = 1; 
		} else {
			this.show(this.current_element + 1);
			this.current_element++; 
		}		
	}
}

function explode(delimiter, string, limit) {
    // Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.  
    // 
    // version: 1009.2513
    // discuss at: http://phpjs.org/functions/explode    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
     var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||        typeof arguments[1] == 'undefined' ) {
        return null;
    }
 
    if ( delimiter === '' ||        delimiter === false ||
        delimiter === null ) {
        return false;
    }
     if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||
        typeof string == 'object' ) {
        return emptyArray;    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;    }
}

function implode(glue, pieces) {
    // Joins array elements placing glue string between items and return one string  
    // 
    // version: 1009.2513
    // discuss at: http://phpjs.org/functions/implode    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Waldo Malqui Silva
    // +   improved by: Itsacon (http://www.itsacon.net/)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: implode(' ', ['Kevin', 'van', 'Zonneveld']);    // *     returns 1: 'Kevin van Zonneveld'
    // *     example 2: implode(' ', {first:'Kevin', last: 'van Zonneveld'});
    // *     returns 2: 'Kevin van Zonneveld'
    var i = '', retVal='', tGlue='';
    if (arguments.length === 1) {        pieces = glue;
        glue = '';
    }
    if (typeof(pieces) === 'object') {
        if (pieces instanceof Array) {            return pieces.join(glue);
        }
        else {
            for (i in pieces) {
                retVal += tGlue + pieces[i];                tGlue = glue;
            }
            return retVal;
        }
    }    else {
        return pieces;
    }
}

