// JavaScript Document
function actionResponse(data){
	if(data.msg.length>0)
		message.show(data.msg);	
}

function submitFormData(frmobj,response){
	if(response.length==0) response = function(){}
	url = $(frmobj).attr("action");
	$.ajax({
			type:"POST",
			url: url,
			data:$(frmobj).serialize(),
			beforeSend:function(){
				message.process("#process#Saving information. Please wait...");
			},
			dataType:'json',
			success:response		
		   })
	return false;
}

var message = {		show:function(msg){	
						this.element = $("#infobar");					
						msg = message.getdata(msg);
						if(msg.length>0){
							$(message.element).click(function(){
									$(message.element).fadeOut("fast");
							})
							
							var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
							$(message.element).css({position:pos})
							setTimeout(function(){$(message.element).html(msg).show().animate({bottom:0}, 100)},600);
							setTimeout(function(){
													$(message.element).animate({bottom:($(message.element).height()+2) * -1},400,function(){/*$(message.element).remove()*/});
												},6000);
						}//endif
					},//show
					getdata:function(msg){
						if(msg.search(/#process#/i)==0){
							//$(message.element).addClass("user_process");
							$(message.element).addClass("user_notice");
							msg = msg.replace(/#process#/i,"");
							msg = "<img src='"+SITE_URL+"images/icons/wait_icon.gif'>&nbsp;&nbsp;"+msg;
						}else if(msg.search(/#info#/i)==0){
							//$(message.element).removeClass('user_process');
							$(message.element).removeClass('user_warning').addClass("user_notice");
							msg = msg.replace(/#info#/i,"");
							msg = "<img src='"+SITE_URL+"images/icons/success_icon.gif'>&nbsp;&nbsp;"+msg;
						}else if(msg.search(/#error#/i)==0){
							msg = msg.replace(/#error#/i,"");
							msg = "<img src='"+SITE_URL+"images/icons/errorIcon.png'>&nbsp;&nbsp;"+msg;
							$(message.element).addClass("user_warning");
						}
						return msg;
					},
					process:function(msg){
						msg = message.getdata(msg);
						if(msg.length>0){
							var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
							$(message.element).css({position:pos})
							setTimeout(function(){$(message.element).html(msg).show().animate({bottom:0}, 100)},600);
						}//endif						
					}
			}//message
			
<!------------------------Mange Category------------------------>			
	function imagePreview(){	
		$("a.preview").live('mouseover mouseout',function(e){
			if(e.type=='mouseover'){
				var url = $(this).attr("url");
				var top = (e.pageY-60)+"px";
				var left = (e.pageX+10)+"px";
				$("<p id='preview'><img alt='Loading..' src='"+url+"'></p>").appendTo("body");
				$("#preview").css({
								top:top,
								left:left,
								position:"absolute",
								border:"2px solid #CECECE",
								background:"#333",
								color:"#ffffff",
								padding:"5px"
				}).hide().fadeIn(400);
			}else{
				$("#preview").remove();
			}
		})//end hover
	}//end function imagePreview
<!------------------------Mange Category------------------------>			
	function tooltip(){
		var active = 0;
		$("a.tooltip").hover(function(e){
			$("<p id='helpText'><img src='"+SITE_URL+'images/admin/tip.gif'+"' class='tip'/>"+$(this).attr('info')+"</p>").css({
				top:e.pageY+10,
				left:(e.pageX-10)+"px"
			}
			).appendTo("body").fadeIn(400);

		},function(){
			//$(".tip").remove();
			$("#helpText").remove();	
			
		})
		
	}//end function tooltip
	
function showLoginBox(){
	$("#loginbox").css({
		position:"absolute",
		top: "32%",
		left:"30%"
	})
	/*
	$("#loginbox").animate({
		opacity:"show",
		left:"33%",
		top:"+=30%"
	},700,function(){$("#loginbox").effect("bounce", { times:2, distance:8 })});*/
	
	$("#loginbox").fadeIn(2500);
}//end function showLoginBox

function loadIcons(){
	var counter = 400;
	$(".icon-col-1").each(function(){
		var element = $(this);
		setTimeout(function(){
			element.fadeIn(600);
		},counter);
		counter = counter+300;	
	})
	
	setTimeout('$(".adminInfo").fadeIn("3000");',2200);
}//end function loadIcons



function fnPageContent() {
	////////// Code for setting layout of template 1 or template 2  of onchange of select template dropdown
	$('#sel_template').live("change", function(){
		var value = this.value; 									   
		template_selected = value;
		//var editorInstance = FCKeditorAPI.GetInstance('txtcontent');
		//alert(value);
		/*if(value!=1 && value!='') {
			editorInstance.EditorWindow.parent.FCKToolbarItems.LoadedItems['employment'].Enable();
		} else {
			editorInstance.EditorWindow.parent.FCKToolbarItems.LoadedItems['employment'].Disable();
//			setTimeout("FCKeditorAPI.GetInstance('txtcontent').EditorWindow.parent.FCKToolbarItems.LoadedItems['employment'].Disable();",500);
		}*/
		if(value!=1 && value) {
			$('#template2').slideDown(1500);
			$('#pdfContainer').slideDown(1500);
			$('#cms_module').slideDown(1500);
			$('#imageContainer').slideDown(1500);
		} else { 
			if($('#template2').is(":visible")) {
				$('#template2').slideUp(1500);
			}
		}
	});
	////////////ends 
	
	////////// Code for remove image using ajax after clicking remove image
	$('.remove_img').live("click",function() {
		var id = this.id; 
		var confirm_msg = "Are you sure you want to remove this image?";
		jConfirm(confirm_msg,'', function(flag) {
			if(flag) {
				/// Call jax
				$.ajax({
					url: imageRemoveUrl,
					data: 'id='+id+'&action=remove',
					type: 'post',
					dataType: 'html',
					onSubmit:  function() {
					},
					success: function (data) {  //alert(data);
						var output = data.split('####');
						if(output[0]=='success') {
							///////////////// Display updated image data
							$('#imageContent').html('<div class="loading">Processing Request<br/><br/>Please Wait....<br/><br/><img src="'+SITE_URL+'images/loading2.gif" /></div>');
							var url = imageContentUrl; 
							setTimeout( function() {$('#imageContent').load(url); }, 1000);
						} else {
							alert(data);
						}
					},
					error: function(err) { 
						alert(err); 
					}//pdf.error
				}); /// Ajax ends here
			} // end confirm if 
			else {
				return false;
			}
		});
		return false;
	});
	//////// ends
	
	////////// Code for remove PDF using ajax after clicking remove PDF icon	
	$('.remove_pdf').live("click",function() {
		var id = this.id; 
		var confirm_msg = "Are you sure you want to remove this pdf?";
		jConfirm(confirm_msg,'', function(flag) {
			if(flag) {
				/// Call jax
				$.ajax({
					url: pdfRemoveUrl,
					data: 'id='+id+'&action=remove',
					type: 'post',
					dataType: 'html',
					
					onSubmit:  function() {
					},
					success: function (data) {  //alert(data);
						var output = data.split('####');
						if(output[0]=='success') {
							///////////////// Display updated image data
							$('#pdfContent').html('<div class="loading">Processing Request<br/><br/>Please Wait....<br/><br/><img src="'+SITE_URL+'images/loading2.gif" /></div>');
							var url = pdfContentUrl;
							setTimeout( function() {$('#pdfContent').load(url); }, 1000);
						} else {
							alert(data);
						}
					},
					error: function(err) { 
						alert(err); 
					}//pdf.error
				}); /// Ajax ends here
			} // end confirm if 
			else {
				return false;
			}
		});
		return false;
	});
	//////// ends
	
	///////// Function for display message after image is removed
	function displayMsg(msg) {
		$('.image-message').animate({
			height: '30px'
		}, $('.image-message')
			.html($('<div class="img-success"></div>').append(msg)).fadeIn(200));
	}
	
	imagePreview();
	
	///////////////////// Code for hide and show of manage SEO section 
	$(".table3row3").click( function() {		
		var panelId = $(this).attr("panel"); 
		$("#"+panelId).slideToggle(1000);
		var currentIconImage = $(this).find('#icon_img').attr("src");
		if(currentIconImage == SITE_URL+'images/admin/minusIcon.jpg'){ 
			$(this).find('#icon_img').attr("src", SITE_URL+'images/admin/plusIcon.jpg');
			//$("#add_seo").html('[+]');
			$(this).find('#icon_img').attr('title','Show');
		}else if(currentIconImage == SITE_URL+'images/admin/plusIcon.jpg'){			
			$(this).find('#icon_img').attr("src", SITE_URL+'images/admin/minusIcon.jpg');
			//$("#add_seo").html('[-]');
			$(this).find('#icon_img').attr('title','Hide');
		} 
	
	});
	/////////////// ends here
}


function changeLayout(value) { 
	//var editorInstance = FCKeditorAPI.GetInstance('txtcontent'); 
	//alert(value);
	if(value!=1 && value) {
			$('#template2').show();
			$('#pdfContainer').show();
			$('#cms_module').show();
			$('#imageContainer').show();
	} else { 
			if($('#template2').is(":visible")) {
					$('#template2').hide("slow");
			}
	}
}  ///// Function ends here


function loadDatePicker() {
	$("#txtDate").datepicker({
		showOn: 'button',
		buttonImage: SITE_URL+'images/calendar.gif',
		buttonImageOnly: true,
		showAnim: 'slideDown',
		dateFormat: 'yy-mm-dd',
		buttonText: 'Click Here'
	});
}

function updateFCKData() {
	$("#selModule_"+emp_module_id).click(function() { 
	var oEditor = FCKeditorAPI.GetInstance('txtcontent') ;    // Get the editor instance that we want to interact with
	var contents =  oEditor.GetXHTML( true ) ;
	
	var multipleValues = $("#selModule_"+emp_module_id).attr('checked');
	if(multipleValues) {
		oEditor.InsertHtml("<p>[CAREER_OPPORTUNITY]</p>");
	} else {
		contents = contents.replace("<p>[CAREER_OPPORTUNITY]</p>","");
		contents = contents.replace("<p>[CAREER_OPPORTUNITY1]</p>","");
		contents = contents.replace("[CAREER_OPPORTUNITY]","");
		contents = contents.replace("<p>&nbsp;</p>","");
		oEditor.SetData(contents);
	}
	
});
}
/* function categoryTree: load tree view of categories or page navigation*/
function categoryTree() {
	//Default Action
	$("#catTree ul:first").addClass("quickTree12");
	$('#catTree ul.quickTree12').quickTree();
	
}
/*	funtion settings: Load tabs and hide other tabs when click on a specific tab.*/
function settings() {
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tabs").attr("selected_form",$("ul.tabs li:first").find("a").attr("href"));
	$(".tab_content:first").show(); //Show first tab content
	
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(".tabs").attr("selected_form",activeTab);
		$(activeTab).fadeIn(400); //Fade in the active content
		return false;
	});
}
/**	function makeSortableTable - Make a list table sortable.
		Parameters: pass page url for which you want to make the table sortable.
*/
function makeSortableTable(pageUrl,param){
	if(!param) param = '';
	$('#table-1').tableDnD({
		onDrop: function(table, row) {
			var order = $('#table-1').tableDnDSerialize() + '&p='+param+'&offset='+offset; 
			//alert(param)
			if(jQuery.tableDnD.status==false){
				//alert(pageUrl)
				$.ajax({
					type:"POST",
					url: SITE_URL+pageUrl,
					data: order,
					beforeSend:function(){
						message.process("#process#Updating record position. Please wait...");
					},
					success: function(res){
						//alert(res)
						var cells = $("td.dragHandle", $('#table-1'));
						var i=(offset*mPagelimit)+1;
						cells.each(function(){
							$(this).next("td").next("td").html(i);i++;
						});
						
						if(res=='true'){
							message.show("#info#Record position updated successfully.");	
							$("#"+row.id).find("input").attr('checked',false);
						}else{
							message.show("#error#Record position can not be updated.");	
						}
					}
				});
			}
			jQuery.tableDnD.status = null;
		},
		dragHandle: "dragHandle"
	});
}
/**
	function makeSortableTable ends here
*/

function display_homepage() {
	$('#display_homepage').click( function(){
		if(this.checked) {
			$('#txtheading').attr( 'require', '1' );
			$('#txtdesc').attr( 'require', '1' );
			$('#displayHomepage').show();
		} else {
			$('#txtheading').attr( 'require', '0' );
			$('#txtdesc').attr( 'require', '0' );
			$('#displayHomepage').hide();
		}
	});	
}

function disableFCKButton() { 
	var oeditorinstatnce = FCKeditorAPI.GetInstance('txtcontent');	
	oeditorinstatnce.EditorWindow.parent.FCKToolbarItems.LoadedItems['employment'].Disable();
	//var contents =  oEditor.GetXHTML( true ) ; alert("content=="+contents);
}
function loadSearchDatePicker() {
	$("#txtFromDate").datepicker({
		showOn: 'button',
		buttonImage: SITE_URL+'images/calendar.gif',
		buttonImageOnly: true,
		showAnim: 'slideDown',
		dateFormat: 'yy-mm-dd',
		buttonText: 'Click Here'
	});
	$("#txtToDate").datepicker({
		showOn: 'button',
		buttonImage: SITE_URL+'images/calendar.gif',
		buttonImageOnly: true,
		showAnim: 'slideDown',
		dateFormat: 'yy-mm-dd',
		buttonText: 'Click Here'
	});

}

function autocompleteSearch(mPageName) {
	$("#search").autocomplete(SITE_URL+'main/list/', {
		width: 500,
		matchContains: true,
		minChars: 0,
		mustMatch: true,
		//minChars: 0,
		//multiple: true,
		//highlight: false,
		//multipleSeparator: ",",
		selectFirst: true
	});
}

function clrsearch(val,act){ 
	if(val=="Search Page"){
		if(act=='f')
			$("#search").val('');
	}
	if(act=='b'){
		if(val == 'Search Page' || val == '')  
			$("#search").val('Search Page'); 
	}
}

function ajaxSwitchModeTo(mode, mfield, id){
	$.ajax({
		   	type:'POST',
			data:'mode='+mode+'&field='+mfield+'&id='+id+'&',
			url: SITE_URL+'admin/main/switchModeFeatured',
			beforeSend:function(){
				message.process("#process#Processing request. Please wait...");
			},
			success:function(res){
				if(res=='true'){
					message.show("#info#Record updated successfully.");	
				}else{
					message.show("#error#Record can not be updated.");	
				}				 	
			}//success
		   })//ajax	
}

function switchStatus(){
	$(".jstar_act").click(function(){
		var obj = $(this);
		var mfield = $(this).attr('field');
		var id = $(this).attr('id');
		if(obj.attr("mode")==1){
			jConfirm("Are you sure you want to change the status?",'', function(flag) {
				if(flag==true){
					obj.attr("mode",0);
					obj.attr("title","Make Featured");
					obj.attr('src', SITE_URL+'images/icons/star_0.gif');
					ajaxSwitchModeTo(0, mfield, id)
				}			  
			});//jConfirm
		}else{
			jConfirm("Are you sure you want to change the status?",'', function(flag) {
				if(flag==true){
					obj.attr("mode",1);
					obj.attr("title","Remove Featured");
					obj.attr('src', SITE_URL+'images/icons/star_1.gif');	
					ajaxSwitchModeTo(1, mfield, id)
				}																	
			})//jconfirm
		}//if mode
	});	//click
}

function initializeGmap(lat, longi, address) {
  if(lat!='' && longi!=''){
	  if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(lat, longi), 13);
		var point = new GLatLng(lat, longi);        
		map.addOverlay(createMarker(point, map, address));		
		map.setUIToDefault();
	  }
  }else{
	  $('#map_canvas').css('width',450);
	  $('#map_canvas').css('height',50);
	  $('#map_canvas').css('text-align','center')
	  $('#map_canvas').html('No map available');
  }
}
function createMarker(point, map, address){
	var marker = new GMarker(point);		
	GEvent.addListener(marker, "click", function() {
		map.openInfoWindow(point, document.createTextNode(address));
	});		
	
	return marker;
}
