//send address with ajax (used for fav etc)
function sendAjax(url) { $('#ajaxload').load(url); }

$(document).ready(function(){
						   
	//AJAX SETUP
	$.ajaxSetup({
  		dataType: "html",
		type: "GET",
		timeout: 60000,
		error: function() { console.log("Error loading document"); }
	});
	
	//DRP DOWN SUCKERFISH STYLE
    $("div.sfclicker").click(function() {
					$(this).children("ul.sdropdown").toggle();	
					$(this).toggleClass("openMenu");
									})
	
	$("body").click(function(e) {
                var clicked = $(e.target); //which element is clicked
                if (clicked.is('.menuitem') || clicked.parents().is('.menuitem') || clicked.is('.sfclicker') || clicked.parents().is('.sfclicker')) {
				} else {   
					$('ul.sdropdown').hide();
					$("div.sfclicker").removeClass("openMenu");
				}
        });	
	
	nextclass = "";
	firstnum = "";

	//first letter for companylist
	$("table#companylist tbody tr").each(function() {
						var firstclass = $(this).attr("rel");
						
						if (isNaN(firstclass)) {
							if(firstclass != nextclass) {
									nextclass = firstclass;
									$(this).attr("id",firstclass) //before("<tr id='"+firstclass+"' class='alpharow'><td>"+firstclass+"</tr></tr>");
									$("div#alphalist").find("a[title="+firstclass+"]").addClass("active").attr("href","#"+firstclass);								
								}
						} else {
							
							if (firstnum != 1) {
									$(this).attr("id","num");
									$("div#alphalist").find("a[title=num]").addClass("active").attr("href","#num");
									firstnum = 1;		
							}
						}
	});
	
	//toggle blur function
	$("#searchblur").click(function () {
								$("span.blur","#searchblur").toggle();
									if( $("span.blur.off","#searchblur").is(":hidden") ) {
											$("input[name='searchonblur']").val("1");
											$("form[name='searchform_inline']").submit();
									} else {
										$("input[name='searchonblur']").val("0");;
									}
								});
	
	
	//keywords show/hide
	$("#searchkeywords").click(function() {
									$("div.searchcheck").slideToggle();
									$("span.blur","#searchkeywords").toggle();
											if( $("span.blur.off","#searchkeywords").is(":hidden") ) {
												$("input[name='includekeywords']").val("1");
											} else {
												$("input[name='includekeywords']").val("0");
											}
										 });
	
	$("input[name='search']").blur(function () {
							if($("span.blur.off","#searchblur").is(":hidden")) {
								$("form[name='searchform_inline']").submit(); }
							});
	
	$("input[type='checkbox']").click(function () {
							if($("span.blur.off","#searchblur").is(":hidden")) {
								$("form[name='searchform_inline']").submit(); }
							});
	
	//delete favourite btn
	$("div.deletefav").click(function() {
								   $(this).parent().parent().fadeOut("slow");
								   });
	
	//scroll to top with ease
	$("a.scrolltop").click(function() { $.scrollTo({top:0}, 400); });
	
	//scroll sign in down
	$("a.eventsignin").click(function(e) { 
	
		var fromtop = e.pageY-120; 
		$("div.loginBubble").animate({top:fromtop}, 400); 
		return false; 
		
	
	});
	
	//CHECK IS COMPANY NAME ON MY PAGE IS CHANGED
	var compval = $("input[name='company']").val();
	
	$("input[name='sendit']").click(function() {
			if ($("input[name='company']").val() != compval) {

				$("div.changedcompanymsg").show();
				$("div.changedcompanymsg ul li input").click(function() {
					$("#updateMyPage").submit();
																			})
				return false;
				}
											  });
	
		// CLUETIPS
		$.cluetip.setup ({
			  insertionType:    'appendTo', // how the clueTip is inserted into the DOM
											// possible values: 'appendTo', 'prependTo', 'insertBefore', 'insertAfter'
			  insertionElement: 'body'      // where in the DOM the clueTip is to be inserted 
			});
		
		//CLUETIP		
		$('a.loadlocal').cluetip({local:true, cursor: 'pointer', activation: 'click', topOffset: 20, dropShadow: false, width:270, leftOffset: -190, positionBy: 'fixed'});
		$('a.tip').cluetip({cluetipClass: 'jtip', local: false, cursor: 'question', activation: 'hover', topOffset: 20, dropShadow: false, width:270, leftOffset: -100, positionBy: 'fixed'});
	
	//read more for reports under country
	$("a.readmore_reports").click(function() {
			$(this).parent().parent().next().toggle();										  
										  });
	
	//add and delete from favourites
	$("a.editfavourites").click(function() {
				//diff animation to make it slide and then show everything
				$("a.editfavourites").toggle();
				$("#favalert p").hide();
				
				var w = $(this).attr("id");
				var href = $(this).attr("href");
				
				$("."+ w +"_text").show();
				$("#favalert").slideDown(500, function() { $("#favalert").animate({opacity:100}, 500, function() { $("#favalert").slideUp(500, function (){ }); }); });				 		 
	
				editFavourites(this);
				return false;
	});
	
	$("img.togglesub").click(function() {
					$(this).parents("tr").find("img.togglesub").toggle();	
					$(this).parents("tr").find("div.subsidiaries").toggle();
									  });
	
	// '''''''' SUPER USER ''''''''''
	//Delete users when you are a super user
	$("a#deleteusers").click(function() {
				$("td.editcell","table.colleaguestable").children().hide();	
				$("td.editcell","table.colleaguestable").children("img.deletebtn").show();
									  });
	
	$("img","th.editrow").click(function() {
				$("div#editmodemsg").fadeOut(500);												   
												   });
	
	$("a#deleteusers","th.editrow").click(function() {
				$("span","div#editmodemsg").remove();
				$("div#editmodemsg").append("<span>You are now in edit mode</span>");
				$("div#editmodemsg").fadeIn(500);
												   });
												   
	$("a.head","div.iorcollapse").click(function() {
		$(this).toggleClass("open");
		$(this).parents("div.iorcollapse").find("div.iorchild").toggle();
		});
	
});