function toggleTextOut(e,txtval) {
	if(e.value=="") e.value = txtval;

}
function toggleTextIn(e,txtval) {
	if(e.value==txtval) e.value="";
	else e.select();
}
$(document).ready(function(){
	$("form ul li").removeClass('clear');
	$(".swap").hover(
		function(){
			if($(this).attr("src").indexOf("-on") == -1) {
			var newSrc = $(this).attr("src").replace(".gif","-on.gif");
			$(this).attr("src",newSrc);
			}},
			function(){
			if($(this).attr("src").indexOf(".gif") != -1) {
			var oldSrc = $(this).attr("src").replace("-on.gif",".gif");
			$(this).attr("src",oldSrc);
		}
	});
	$("input.combo").toggle(function(){
		$('.cat-content').show(400);
	},function(){
		$('.cat-content').hide(400);
	});
	$('a.sblogin').click(function(){
		$('.loginsb').hide();
		$('.wrap-login').slideToggle();
	});
	$('a.close').click(function(){
		$('.loginsb').show();
		$('.wrap-login').hide();
		$('.contribute').slideUp(600);
	});
	$('a.lg-contribute').click(function(){
		$('.contribute').slideToggle(600);
	});
	$('ul.filmstrip li').addClass('filmstripnode');
	$(".tabbed_area").find(".content-box:first").show();
	$("a.tab").click(function () {
		var scope = $(this).parents(".tabbed_area");
		
			// switch all tabs off
				$('.active',scope).removeClass("active");

				// switch this tab on
				$(this).addClass("active");

				// slide all content up
				$(".content-box",scope).hide();

				// slide this content up
				var content_show = $(this).attr("rel");
				$("#"+content_show).fadeIn(600);

	});
	
	$('#nav-top ul li ul').addClass('subnav');
	$("#nav-top ul.subnav").parent().append("<span>more</span>"); 
	     $("#nav-top ul li span").click(function() { //When trigger is clicked...  
	         //Following events are applied to the subnav itself (moving subnav up and down)  
	         $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click  
	         $(this).parent().hover(function() {  
	         }, function(){  
	             $(this).parent().find("ul.subnav").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up  
	         });  
	   
	         //Following events are applied to the trigger (Hover events for the trigger)  
	         }).hover(function() {  
	             $(this).addClass("subhover"); //On hover over, add class "subhover"  
	         }, function(){  //On Hover Out  
	             $(this).removeClass("subhover"); //On hover out, remove class "subhover"  
	});
	
	$('ul.static-rate li:odd').addClass('odd');
	$("input.combo").toggle(function(){
	  $('.cat-content').show(400);
	},function(){
	   $('.cat-content').hide(400);
	});
	$('.boxgrid.caption').hover(function(){
		$(".cover", this).stop().animate({top:'170px'},{queue:false,duration:160});
	}, function() {
		$(".cover", this).stop().animate({top:'200px'},{queue:false,duration:160});
	});
	
	var tabContainers1 = $('div.searchFormTab');
	$('div.search-tabs ul.search-list a').click(function () {
		tabContainers1.hide().filter(this.hash).show();
	    $('div.search-tabs ul.search-list li').removeClass('current').removeClass('no-bg');
	    $(this).parents("li:first").addClass('current').next('li').addClass('no-bg');
	    return false;
	});
	
	var tabContainers2 = $('div.tab-content');
	$('div.tab-container ul.nav-tab a').click(function () {
		tabContainers2.hide().filter(this.hash).show();
	    $('div.tab-container ul.nav-tab li').removeClass('current');
	    $(this).parents("li:first").addClass('current');
	    return false;
	}).filter(':first').click();

	// For print
	$('a.print').click(function(){
		window.open(this.href,"_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width=604px, height=600");
		return false;
	});
	
	// --Start-- For header combo select options
	$(".comboActionCheckAll").click(function(){
		$("input.comboCheckAll").attr("checked",true);
		return false;
	});
	
	$(".comboActionUnCheckAll").click(function(){
		$("input.comboCheckAll").attr("checked",false);
		return false;
	});
	
	$(".comboActionHideAll").click(function(){
		$(this).parents("div.comboDiv:first").hide();
		return false;
	});
	
	$('.comboCheckAll').click(function(){
		if($(this).attr('checked')) {
			$(this).parents("li:first").find(".comboCheckAll").attr("checked",true);
		} else {
			$(this).parents("li:first").find(".comboCheckAll").attr("checked",false);
		}
	});
	// --End-- For header combo select options
	
	//Calendar fixes
	$('#event_startDate_date').attr('title','Select date').click(function(){
		$('#event_startDate_date').datepicker('show');
	});
	$('#event_endDate_date').attr('title','Select date').click(function(){
		$('#event_endDate_date').datepicker('show');
	});
	// For field title overlays
	$('input.title-overlay').fieldTitleOverlay();
	
	// For autocomplete actions.
	var autoSuggestOptions = { 	
		delay:5, minChars:2, matchSubset:1, matchContains:1, cacheLength:10,
		resultsClass:'autosuggest',
		onItemSelect: function(li){
			if(li.extra){
				if(li.extra[1]){
					return false;
				}else{
					window.location = li.extra[0];
				}
			}else{
				return false;
			}
		},
		formatItem: function(row, i, max, value, term){
			return row[0];
		},
		autoFill:false,
		autoCompleteCallback:1,
		multiple: true,
		multipleSeparator: ","
	};
	$(".dir-type-autosuggest").cmAutocomplete(
		"/en/common/autoCompleteDirType/",autoSuggestOptions
	);
	$(".blog-type-autosuggest").cmAutocomplete(
			"/en/common/autoCompleteBlogType/",autoSuggestOptions
	);
	$(".incident-type-autosuggest").cmAutocomplete(
			"/en/common/autoCompleteIncidentType/",autoSuggestOptions
	);
	$(".event-type-autosuggest").cmAutocomplete(
			"/en/common/autoCompleteEventType/",autoSuggestOptions
	);
	$(".area-autosuggest").cmAutocomplete(
			"/en/common/autoCompleteAreaType/",autoSuggestOptions
	);
	
	// Date picker
	Date.format = 'mm/dd/yyyy';
	var today = new Date();
	var yesterDay = today.getTime() + (60 * 1000 * 60 * 24);
	endDateToSet = (new Date(yesterDay)).asString();
	//endDateToSet = (new Date()).asString();
	$('.cal-date').datePicker({startDate: '01/01/1970',clickInput:true,endDate: endDateToSet});
	$('.dp-choose-date').hide();
	
	//Loading the Google map
	$("a.map-link").click(function(){
		if($(this).hasClass('map-close')){
			$.get('/en/common/loadMapData/',
				{
					keyWord:'test',
					location: 'location',
					withMap: false
				},
				function(data){
					$(".map-container").html(data).show();
				}
			);
			$(this).removeClass('map-close').addClass('map-open');
		} else {
			$(".map-container").html('').hide();
			$(this).removeClass('map-open').addClass('map-close');
		}
	});
	
	$(".box-message").fadeIn(1000,function(){
		$(this).fadeOut(4000,function(){
			$(this).remove();
		});
	});
	
	// Been here button
	$(".login-required").click(function(){
		$(".been-here-message").removeClass('success').addClass("error").show().fadeIn(2000,function(){
			$(this).fadeOut(5000,function(){
				$(this).remove();
			});
		}).find("div.dialog-content").html('Please login to use this operation!');
		$('a.sblogin').click();
		return false;
	});
	$(".mark-beenhere").click(function(){
		$.get($(this).attr('href'),'',function(data){
			if(data=="added") {
				$(".been-here-message").removeClass('error').addClass("success").show().fadeIn(2000,function(){
					$(this).fadeOut(5000,function(){
						$(this).remove();
					});
				}).find("div.dialog-content").html("Thank you for your information!");
				$(".been-here-callout").hide();
			}
		});
		return false;
	});
	
	// Form input highlight class
	/*
		$("div.form :input, div.form :select, div.form :textarea, div.form table").bind("focus", function(){
			$(this).parents('li:first').addClass('focussed');
		});
		$("div.form :input, div.form :select, div.form :textarea, div.form table").bind("blur", function(){
			$(this).parents('li:first').removeClass('focussed');
		});
	*/
	
	// Form notes to add on all forms
	$('#event_startDate_time,#event_endDate_time,#citizenPolicing_incidentDateTime_time').addClass('timeSelect').after('<a href="#" class="timeSelectLink">Select</a>');
	$(".timeSelectLink").click(function(){
		$(this).prev(':first.timeSelect').click();
		return false;
	});
	
	$("#wrap_directory_taxonomy_5 label,#wrap_citizenPolicing_taxonomy_5 label").after('<small class="i-error instruct3">Please type in a few letters to search (e.g., "Bandr" "Juh")</small>');
	//Event form
	$("#wrap_event_taxonomy_6_6 ul.checkboxes").before('<small class="i-error instruct">Please select event type from below, you can select more than one.</small>');
	$("#wrap_event_eventLocation label").after('<small class="i-error instruct">Please type in a few letters to search (e.g., "NCPA" "Zenzi")</small>');
	$("#event_startDate").after('<small class="i-error instruct1">Please select event start date here.</small>');
	$("#event_startDate").after('<small class="i-error instruct2">Please select event start time here.</small>');
	$("#event_endDate").after('<small class="i-error instruct1">Please select event end date here.</small>');
	$("#event_endDate").after('<small class="i-error instruct2">Please select event end time here.</small>');
	
	$("#event_startDate_date,#event_startDate_time,#event_endDate_date,#event_endDate_time").attr('readonly',true);
	if(!$("#event_startDate_date").val()) {
		setDate('event_startDate_date');
	}
	if(!$("#event_startDate_time").val()) {
		setTime('event_startDate_time');
	}
	if(!$("#event_endDate_date").val()) {
		setDate('event_endDate_date',(60 * 1000 * 60));
	}
	if(!$("#event_endDate_time").val()) {
		setTime('event_endDate_time',(60 * 1000 * 60));
	}
	$('#event_startDate_date').bind('change',function(){
		if(!$('#event_startDate_date').val()) {
			setDate('event_startDate_date');
		}
		if(!$('#event_startDate_time').val()) {
			$('#event_startDate_time').val("00:00");
		}
		setDate('event_endDate_date',(60 * 1000 * 60),$('#event_startDate_date').val(),$('#event_startDate_time').val());
		setTime('event_endDate_time',(60 * 1000 * 60),$('#event_startDate_date').val(),$('#event_startDate_time').val());
	});
	$('#event_startDate_time').bind('change',function(){
		console.log("Here");
		if(!$('#event_startDate_date').val()) {
			setDate('event_startDate_date');
		}
		if(!$('#event_startDate_time').val()) {
			$('#event_startDate_time').val("00:00");
		}
		setTime('event_endDate_time',(60 * 1000 * 60),$('#event_startDate_date').val(),$('#event_startDate_time').val());
	});
	$("#event").bind('invalid-form',function(){
		if($("#event_startDate_date").val()) {
			$("#event_startDate").val($("#event_startDate_date").val());
		} else {
			$("#event_startDate_date").val($("#event_startDate").val());
		}
		if($("#event_startDate_date").val()) {
			$("#event_endDate").val($("#event_endDate_date").val());
		} else {
			$("#event_endDate_date").val($("#event_endDate").val());
		}
	});
	//Blog form
	$("#wrap_BlogPost_taxonomy_2_2 ul.checkboxes").before('<small class="i-error instruct">Please select blog type from below, you can select more than one.</small>');
	//Incident form
	$("#wrap_citizenPolicing_taxonomy_5").addClass('sl-nobg');
	$("#citizenPolicing_incidentDateTime").after('<small class="i-error instruct1">Please select incident start date here.</small>');
	$("#citizenPolicing_incidentDateTime").after('<small class="i-error instruct2">Please select incident start time here.</small>');
	$("form#citizenPolicing .google-map-field").parents('li:first').addClass('sl-nobg')
	//Directory form
	$("#wrap_directory_taxonomy_12 label").after('<small class="i-error instruct3">Please type in a few letters to search (e.g., "clubs" "cafe")</small>');
	$("#wrap_directory_taxonomy_3 label").after('<small class="i-error instruct3">Please type tag u want to apply and enter</small>');
	
});
function setSectionLinkActive(linkClass) {
	if(linkClass) {
		$("#nav-top ul li").removeClass('current');
		$("#nav-top ul li."+linkClass).addClass('current');
	}
}
function setDate(elem, diff, dateBase, timeBase){
	if(dateBase) {
		var dateArr = dateBase.split("-");
		var dateStr = dateArr[1]+"/"+dateArr[2]+"/"+dateArr[0]+" "+timeBase;
	}
	if(diff) {
		if(dateStr) {
			var today = new Date(dateStr);
		} else {
			var today = new Date();
		}
		var dateInMS = today.getTime() + diff;
		var dateToUse = new Date(dateInMS);
	} else {
		if(dateStr) {
			var dateToUse = new Date(dateStr);
		} else {
			var dateToUse = new Date();
		}
	}
	var month = dateToUse.getMonth() + 1;
	var day = dateToUse.getDate();
	var year = dateToUse.getFullYear();
	$("#" + elem).val(year + "-" + month + "-" + day);
}
function setTime(elem, diff, dateBase, timeBase){
	if(dateBase) {
		var dateArr = dateBase.split("-");
		var dateStr = dateArr[1]+"/"+dateArr[2]+"/"+dateArr[0]+" "+timeBase;
	}
	if(diff) {
		if(dateStr) {
			var today = new Date(dateStr);
		} else {
			var today = new Date();
		}
		var dateInMS = today.getTime() + diff;
		var dateToUse = new Date(dateInMS);
	} else {
		if(dateStr) {
			var dateToUse = new Date(dateStr);
		} else {
			var dateToUse = new Date();
		}
	}
	var hours = dateToUse.getHours();
	$("#" + elem).val(hours + ":00");
}

$.fn.rating.options.cancelValue='Clear rating';
function searchboxValidate(msg) {
	sb = document.getElementById('looking_for_something').value;
	if(sb==msg || sb==""){
		document.getElementById('looking_for_something').value = msg;
		document.getElementById('looking_for_something').focus();
		return false;
	}
	return true;
}

function spamFreeEmailLink(user, domain,tag){
	if(tag){
		document.write('<a  target="_blank" href="' + 'mailto:' + user + '@' + domain  + '">' + user + '@' + domain + '</a>');
	}else{
		document.write(user + '@' + domain );
	}
}

// For making the tags with equal hight.
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

$(window).load(function(){
	var classArr = [""];
	if(classArr) {
		for(var i=0;i<classArr.length;i++){
			equalHeight($(classArr[i]));
		}
	}
});

/* JS Using for the iphone app template - STARTS */


$(document).ready(function() {
	
	
   	$(".howto-categories").children('li').children('h3:not(:first)').next().hide();
   	$(".howto-categories").children('li').children('h3:first').addClass('selected');
	$(".howto-categories").children('li').children('h3').click(function(){
		if( !$(this).next().is(':visible')){
			$(".howto-categories").find(".selected").next("div").hide();
			$(".howto-categories").find(".selected").removeClass('selected');
			$(this).addClass('selected');
			$(this).next().slideDown();
		}
		
		return false;
	});
	
	
    $("#slctCatlist").change(function() { 
									  
	   $("#empty_category").css("display","none");
       var id = $("option:selected", this).val();
       var catArray = new Array();
	
		catArray['started'] 	= 'Getting Started';
		catArray['navigation'] 	= 'Navigation';
		catArray['basic'] 		= 'Basic';
		catArray['search'] 		= 'Search';
		catArray['sharing'] 	= 'Sharing and Interactivity';
		catArray['settings'] 	= 'Settings';
		
		if( id != 0 ) {
			$(".howto-categories").css("display","none");
			$("#"+id).children('li').children('h3:not(:first)').next().hide();
			$("#"+id).css("display","block");
			$("#"+id).children('li:first').children('h3').addClass('selected').next('div').show();
			$("#category-title").html( catArray[id] );
		}else {
			$(".howto-categories").css("display","none");
			$("#category-title").html('');
			$("#empty_category").css("display","block");			
		}
    });
        
});

/* JS Using for the iphone app template - ENDS */
	

/*
CSS Browser Selector v0.2.7
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
var css_browser_selector = function() {var ua=navigator.userAgent.toLowerCase(),is=function(t){return ua.indexOf(t) != -1;},h=document.getElementsByTagName('html')[0],b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?'gecko ff2':is('firefox/3')?'gecko ff3':is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';var c=b+os+' js'; h.className += h.className?' '+c:c;}();

