$(document).ready(function() {
	
	//var GLOBAL_URL = "http://localhost/projects/mark/cfawindia";
	var GLOBAL_URL = "http://www.cfaw.com";
	var OTHER_URL = "http://cfaw.com";
	
	var contentheight = $("#mainbody").height();
	$(".award").css({height:contentheight});
	$("#newcfa").dropShadow({blur:0,opacity:0.6,left:2,top:2,color:"#535353"});
	$("#mainmenu ul").supersubs({ 
        minWidth:    12,   // minimum width of sub-menus in em units 
        maxWidth:    27,   // maximum width of sub-menus in em units 
        extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                           // due to slight rounding differences and font-family 
    }).superfish();
	// tabs
	$(".thetab").hide();
	$(".thetab:first").show();
	
	$("#tab a").click(function() {
		var obj = $(this).attr("title");
		$(".thetab").hide();
		$("#"+obj).show();
		
		$("#tab li").each(function() {
			$(this).removeClass("active");
		});
		
		$(this).parent("li").addClass("active");
		
		// get theid
		var theid = $(this).parent("li").attr("id");
		
		// remove class
		$(this).parents("#tab").removeAttr("class").addClass(theid);
		return false;
	});
	
	// images
	$("img,input[@type=image]").each(function() {
		var src = $(this).attr("src");
		if(src.indexOf("[cfa]") != -1) {
		var newsrc = src.replace("[cfa]",GLOBAL_URL);
		$(this).attr("src",newsrc);
		}
	});
	
	// link - for a while
	$("a").each(function() {
		var href = $(this).attr("href");
		
		if(href !== undefined) {
			if(href.indexOf("http") == -1) {
				var newhref = GLOBAL_URL+"/index.php/show/page/"+href;
				$(this).attr("href",newhref);
			}
		}
		
	});
	
	$(".sidebar-left .bgimg").addClass("award");
	
	// contact form
	$("#contact-form").submit(function() {
		var obj = $(this);
		var valid = 1;
		$("#contact-form input[@type=text]").each(function() {
			if($(this).val() == "") {
				alert("Please fill all the fields: "+$(this).attr("name"));
				$(this).focus();
				valid = 0;
				return false;
			}
			else valid = 1;
		});
		
		if(valid) {
			obj.attr({action: GLOBAL_URL+"/index.php/contact/"});
			return true;
		}
		else return false;
		
	});
	if($("#contact-form, #contact-page").length > 0) {
   		var url = GLOBAL_URL+"/index.php?contact/token/";
   		//var url = "http://localhost/projects/mark/cfa/site-editor/index.php/contact/token/";
   		$.get(url,function(vartext) {
   			
   			$("#contact-form, #contact-page").append("<in"+"put type="+"'hidden' name='token' value='"+vartext+"' />");
   		});
   		$("input[@type=text]").each(function() {
   			$(this).attr({value:""});
   		});
   }
   
   // email address
   if($(".email-address").length > 0) {
   		var encoded = $.trim($(".email-address").attr("id"));
   		
   		var useremail = encoded.split("AKEONG");
   		
   		var emailaddress = encoded.replace("AKEONG","@").replace("TITIK",".");
   		var linkcontact = GLOBAL_URL+"/index.php/contact/sendto/"+useremail[0];
   		
   		$(".email-address").empty().append("<a href='"+linkcontact+"'>"+emailaddress+"</a>");
   }
   
   // left side make equal height
   $("#leftcol").css("height",$("#content").height()+"px");
   if($("a.linkedin").length > 0) {
   	$("a.linkedin").css({position:"absolute",bottom:"0px",left:"10%"});
   }
   if($("ul.linkedin").length > 0) {
   	$("ul.linkedin").parent("div.sidebar-modules").addClass("linkedin");
   	$("div.linkedin").css({position:"absolute",bottom:"0px",left:"0px"});
   }
   
   // link

   $("a").each(function() {
   	if($(this).attr("href") != null) {
	   	if($(this).attr("href").indexOf("#") == -1) {}
	   	else {
	   		var thisurl = $(this).attr("href");
	   		var urlnow = window.location;
	   		if(thisurl.indexOf("site-editor") != -1) {
	   			thisurl = thisurl.replace(GLOBAL_URL+"/index.php/show/page/","");
	   			$(this).attr("href",thisurl);
	   		}
	   		
	   	}
   	}
   });

	
	$("select[@name=selectThis]").change(function() {
		var theurl = $(this).val();
		if(theurl != "") {
			document.location.href = theurl;
		}
	});
	
	$("a.dropdown-main").click(function() {
		if($(".dropdown-hide").length > 0) {
			$(".dropdown-item").slideDown("slow").removeClass("dropdown-hide");
			return false;
		}
		else {
			$(".dropdown-item").slideUp("slow").addClass("dropdown-hide");
			return false;
		}
	});
	$("a.dropdown-item").blur(function() {
		$(this).slideUp("slow").addClass("dropdown-hide");
	});
	
	$('a[@href^="http:"]').each(function() {
		var linkurl = $(this).attr("href");
		var theclass = $(this).attr("class");
		var linkname = $(this).text();
		
		var url = "http://cfaw.com/assets";
		var newurl = GLOBAL_URL+"/go.php?to="+linkurl;
		
		if(linkurl.substr(0,GLOBAL_URL.length) != GLOBAL_URL && 
			linkurl.substr(0,OTHER_URL.length) != OTHER_URL) {
			$(this).attr("href",newurl);
			
			if(theclass != "linkedin") {
			$(this).append("&nbsp;&nbsp;<img style='border:0px !Important' src='"+url+"/images/new_window.gif' alt='link' />");
			}
		} 
		
		//if($.browser.msie) $(this).empty().append(linkname);
	});
	
	if($("input[@name=follow_up]").length > 0) {
		/*
		var default_value = $("input[@name=follow_up]").val();
		if(default_value != "null" || default_value != "") {
			$(".your_"+default_value).show();		
		}
		*/
		$("input[@name=follow_up]").click(function() {
			//get value
			var theval = $(this).attr("value");
			$(".follow_up").hide();
			$(".follow_up input").removeClass("required");
			$(".your_"+theval).show();
			$(".your_"+theval+" input").addClass("required");
		});
	}
	if($("#contact-individual-page").length > 0) {
		$("#contact-individual-page").submit(function() {

			var obj = $(this);
			var valid = 1;
			
			$("#contact-individual-page .required").each(function() {
				if($(this).val() == "") {
					//alert("Please fill all the fields: "+$(this).attr("name"));
					$(".loading-info").empty().append("<p>Please fill all the fields: <strong>"+$(this).prev("label").text()+"</srong></p>");
					$(this).focus();
					valid = 0;
					return false;
				}
				else valid = 1;
			});
			if(valid) {
				alert("")
				/*
				if($("input[@name=follow_up]:checked").val() == "") {
					$(".loading-info").empty().append("<p>Please choose one</p>");
					$(".prefer_follow_up").css({background:"#FFFFCC"});
					valid = 0;
				}
				else {
					$(".prefer_follow_up").css({background:"#FEFBEE"});
					valid = 1;
				}
				*/
			}
			
			if(valid) {
				obj.attr({action: GLOBAL_URL+"/index.php/submit_contact/"});
				//return true;
			}
			else return false;
		});
	}
	if($(".pic-slideshow").length > 0) {
		$(".pic-slideshow img").show();
		$('.pic-slideshow').cycle({
			fx: 'fade',
			pause: 1
		});
	}
	
	//search 
	$("#search").hide();
	$("#searchsubmit").attr("value","");
	$("li.search a").toggle(function() {
		$(this).parent("li").addClass("search-hover");
		$("input#s").select().focus();
		$("#search").show();
		return false;
	},function() {
		$(this).parent("li").removeClass("search-hover");
		$("#search").hide();
		return false;
	});
	
	
});

if (document.location.protocol === "https:")
document.icpsignup.action = "https://app.icontact.com/icp/signup.php";

function verifyRequired() {
  if (document.icpsignup["fields_email"].value == "") {
	document.icpsignup["fields_email"].focus();
	alert("The Email field is required.");
	return false;
  }

  if (document.icpsignup["fields_fname"].value == "") {
	document.icpsignup["fields_fname"].focus();
	alert("The First Name field is required.");
	return false;
  }

return true;
}
