jQuery.fn.limitChars = function(limit, infoEl) {
	var text = $(this).val();
  	var textlength = text.length;
	$(infoEl).html(''+ (limit - textlength) +'');
	var limitEl = this;
	
	
	this.keyup(function(){
  		var text = $(this).val();
  		var textlength = text.length;
		if(textlength > limit)
		{
			//$(infoEl).html('You cannot write more then '+limit+' characters!');
			//var scroll = limitEl.scrollTop();
			$(this).val(text.substr(0,limit));
			//limitEl.scrollTop(scroll);
			return false;
		}
		
		$(infoEl).html(''+ (limit - textlength) +'');
		return true;
	});
};

function limitChars(textid, limit, infodiv) {
	var text = $('#'+textid).val(); 
	var textlength = text.length;
	if(textlength > limit) {
		$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	} else {
		$('#' + infodiv).html(''+ (limit - textlength) +'');
		return true;
	}
}

// print function
function printit(){  
	if (window.print) {
		window.print() ;  
	} else {
		var WebBrowser = '<object id="WebBrowser1" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
		WebBrowser1.ExecWB(6, 2); //Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
	}		 
}

$(document).ready(function() {

	// fading images in the header
	if($('#slider').length > 0) {
		$('#slider').s3Slider({
			timeOut: 4000
		});
	}
	
	// currency converter slider
	if($('#widget-currency').length > 0) {
		$('#currency-trigger').click(function() {
			$('#widget-currency').fadeIn(200);
		});	
		$('.widget-currency-close').click(function() {
			$('#widget-currency').fadeOut(200);
		});
	}
	
	if($(".shadowbox").length > 0) {
		Shadowbox.init({
			overlayOpacity: 0.6,
			handleOversize: "drag"
		});
	}
	
	// navigation
	if($("#snv").length > 0) {
		$(".sub-nav").hide();
		$("#snv li.drop").hover(function() {
			$(".sub-nav").stop(true,true).fadeIn(200);
			$(this).children('a').addClass('act');
		}, function() {
			$(".sub-nav").stop(true,true).fadeOut(200);
			$(this).children('a').removeClass('act');
		});
	}
	
	// gallery
	if($('#ul_gallery').length > 0) {
		$('#ul_gallery').cycle({
			fx:     'fade',
			speed:  'fast',
			timeout: 0,
			pager:  '#gal_nav',			
			pagerAnchorBuilder: function(idx, slide) {
				// return sel string for existing anchor
				return '#gal_nav span:eq(' + (idx) + ') a';
			}
		});
	}
	 
	// fading thumbnails
	if($('#gal_nav').length > 0) {
		$('#gal_nav').cycle({
			fx:     'fade',
			speed:   300,
			timeout: 0,
			next: '.gal_nav_next',
			prev: '.gal_nav_prev',
			manualTrump:false,
			pause:1	
		});
	}
	
	
	// featured adverts
	if($('.ul_feat_out').length > 0) {
		$('.ul_feat_out').cycle({
			fx:     'scrollVert',
			speed:   1000,
			timeout: 7000,
			pause: '.ul_feat_out',
			next: '.ul_feat_next',
			prev: '.ul_feat_prev',
			manualTrump:false,
			pause: 1
		}); 
	}
	
	// toggles the slickbox on clicking the noted link (Advanced Search button)
	if($('a#slick-toggle').length > 0) {
		$('a#slick-toggle').click(function() {
			$('.src_advanced').slideToggle(400);
			return false;
		});
	}

	// sliding bookmark
	$('a[href*=#to_]').click(function() {									
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});
	 
	 
	// table cells highlighting
	if($('table.tbl_calendar').length > 0) {
		$('table.tbl_calendar tr').children().click(function() {	
			$('table.tbl_calendar tr').children().removeClass('hov'); 
			var index = $(this).parent().children().index(this); 
			$('table.tbl_calendar tr').each(function() {
				$(':nth-child(' + (index + 1) + ')' ,this).addClass('hov');
			});	
		});
	}
	  
	// Date picker
	if($('.date-pick').length > 0) {
		Date.format = 'yyyy-mm-dd';
		$(function() {
			$('.date-pick').datePicker({clickInput:true});
			$('#src_date_from').bind('dpClosed',function(e, selectedDates) {
				var d = selectedDates[0];
				if (d) {	
					d = new Date(d);
					$('#src_date_to').dpSetStartDate(d.addDays(1).asString());
				}
			});
			$('#src_date_to').bind('dpClosed',function(e, selectedDates) {
				var d = selectedDates[0];
				if (d) {	
					d = new Date(d);
					$('#src_date_from').dpSetEndDate(d.addDays(-1).asString());
				}
			});
		});
	}
	
	
	// characters remaining
	$('textarea[maxlength]').keyup(function(){
		var max = parseInt($(this).attr('maxlength'));
		if($(this).val().length > max){
			$(this).val($(this).val().substr(0, $(this).attr('maxlength')));
		}
		$(this).parent().find('.charsRemaining').html('[ You have ' + (max - $(this).val().length) + ' characters remaining ]');
	});
	
	// make dropdown region disabled
	function disableSelect(trigger,target) {
		if($(trigger).val() == "") {
			$(target).attr("disabled", "disabled");					
		} else {
			$(target).removeAttr("disabled");
		}
	}
	
	// search form
	var se = {
		init: function() {
			$('#se_country').change(function(){
				se.refreshRegion();
			});			
			$('#btn_reset').click(function(){
				se.reset();
				return false;
			});
		},
		refreshRegion: function() {
			var html = $.ajax({
				url: "/?mod=6&action=_06_regions&country="+$('#se_country').attr('value'),
				async: false
			}).responseText;
			
			$('#se_region').empty().append(html);
			// make dropdown region disabled	
			disableSelect('#se_country','#se_region');	 
		},
		reset: function() {
			$('#se_country').val('');
			$('#se_region').val('');
			$('#src_key').val('');
			$('#src_date_from').val('');
			$('#src_date_to').val('');
			$('#se_bedrooms').val('');
			$('#se_property_type').val('');
			$('#se_group_people').val('');
			$('#src_prop_ref').val('');
			$('#src_price_from').val('');
			$('#src_price_to').val('');
			$('#frmsrch').find(':checked').attr('checked',false);
			disableSelect('#se_country','#se_region');				
			return false;
		}		
	}; 
	
	// 'how to' video
	var video = {
		init: function() {
			$('.vid_container').hide();
			$('#li_video_content').click(function(e){
				e.stopPropagation();
				video.showVideo();
				return false;
			});
			
			$('#id_cost_content').hide();
			$('#li_cost_content').click(function(e){
				e.stopPropagation();
				video.showCost();
				return false;
			});
			
			$('#li_non_video_content').unbind().click(function(e){
				e.stopPropagation();
				video.showContent();
				return false;
			});		
		},
		showVideo: function() {			
			$('#li_non_video_content').find('a').removeClass('act');
			$('#li_cost_content').find('a').removeClass('act');
			$('#li_video_content').find('a').addClass('act');
			$('#non_video_content').hide();
			$('#id_cost_content').hide();
			$('.vid_container').show();
		},
		showContent: function() {
			$('.vid_container').hide();
			$('#id_cost_content').hide();
			$('#li_video_content').find('a').removeClass('act');
			$('#li_cost_content').find('a').removeClass('act');
			$('#li_non_video_content').find('a').addClass('act');
			$('#non_video_content').show();
		},
		showCost: function() {
			$('#li_non_video_content').find('a').removeClass('act');
			$('#li_video_content').find('a').removeClass('act');
			$('#li_cost_content').find('a').addClass('act');
			$('#non_video_content').hide();
			$('.vid_container').hide();
			$('#id_cost_content').show();
		}
	}
	
	// print page button
	if($("#print_page").length > 0) {
		$("#print_page").prepend('<div class="sbm sbm_blue"><a href="#" class="btn_flex print">Print this page</a></div>');
		$("#print_page a.print").click(function() {
			window.print();
			return false;
		});
	}
	
	// call function to check if country is selected
	// when form first loads on the page and disable region if necessary
	disableSelect('#se_country','#se_region');
	
	// call search form function
	se.init();
	
	// call video displaying function
	video.init();
	
	
	if ($("#click_here_scroll").length > 0) {
		$("#click_here_scroll").click(function() {
			$("#click_here_scroll_target").slideToggle('fast');
			return false;
		});
	}
	
	
	
	if ($("#screencast_video").length > 0 && $("#screencast").length > 0) {
		$("#screencast_video").change(function() {
			var id = $(this).val();
			$("#screencast").children().each(function() {
				if (!$(this).hasClass(id)) {
					$(this).addClass('dn');
				} else {
					$(this).removeClass('dn');
				}
			});			
		});
	}
		
});
