var rootUrl = "http://www.tangent360.com/";

jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	return this;
};


$.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
	 
};


function anchorLoad(){
	var locc = window.location.toString();
	var lcs = locc.split("#");
	if($.browser.msie===true){
		if(lcs.length == 2){
			anh = lcs[0] + "#" + lcs[1] + "_anc";
			window.location = anh;
		}
	}
}

// ######################################### APPLY CUFON FONTS ######################################################
	Cufon.replace("#menu a,h4,h3,h2,h1, span.featHeader, span.commentHeader", { fontFamily: 'Anivers' });

jQuery(function($){
	
	
	$(".alignright, .alignleft").each(function(i, element){
		if($(this).html() == ""){
			//alert("No next text!");
			$(element).css({background: "none"});
		} else {
			//theOffset = btov_drawer;
		}
	});
	
	$("ul.submenu li, a.a_anc").click(function(e){
		if(this.tagName == "LI"){ 				  
			tid = $(this).attr('id').replace("a-", "#");
		} else if(this.tagName == "A"){ 				  
			tid = $(this).attr('href');
			e.preventDefault();
		}
		if($.browser.msie===true){
			anc = tid + "_anc";
			window.location = anc;
		} else {
			tp = $(tid).offset().top;
			spd = parseInt(tp / 2.5);
			$('html, body').animate({
				scrollTop: tp
			}, spd, 'easeOutQuart');
		}
		
	});
	
	$("a").click(function(e){			  
		if($(this).attr('class').substring(0,7) == 'noclick'){
			e.preventDefault();
		}
		if($(this).attr('rel') == 'external'){
			e.preventDefault();
			window.open(this.href);
			return false;
		}
	});
	
	
	
	//$("a.a_anc").click( function(e){ onClick("hello"); e.preventDefault(); } );
	
	
	$("img.navup").click(function(){
		//alert("Browser: " + $.browser.opera + "\nVersion: " + $.browser.version);
		var locc = window.location.toString();
		var lcs = locc.split("#");						  
		if($.browser.msie===true){
			window.location = lcs[0] + "#cl";
		} else if($.browser.opera===true){
			window.location = lcs[0] + "#cl";
		} else {
			tp = $('#cl').offset().top;
			co = $(this).offset().top;
			//alert("This: " + co + "\nTop: " + tp);
			spd = parseInt(co / 2.5);
			$('html, body').animate({
				scrollTop: tp
			}, spd, 'easeOutQuart', function() {
				if(lcs.length > 1){
					window.location = lcs[0];
				}
			});

		}
		
	});
	
	
	
	
	
	/*
	if($.browser.msie===true && $.browser.version==='7.0'){
		anc = tid + "_anc";
		window.location = anc;
	} else {
		tp = $(tid).offset().top;
		spd = parseInt(tp / 2.5);
		$('html, body').animate({
			scrollTop: tp
		}, spd, 'easeOutQuart');
	}*/
				
	$("img").bind("contextmenu",function(e){
        return false;
    });
	/*
	$(this).bind('keypress', function(e) {
		var key = (e.keyCode ? e.keyCode : e.charCode);
		alert(key);                   
	});
	*/

				
	// ######################################### RESET CSS FROM JS DISABLED DEFAULTS #####################################
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
		$(".feature-left").css({
			"margin-right": "40px"
		});
	} else {
		$(".feature-left").css({
			"margin-right": "80px"
		});
	}
	
	$("#featured").css({		// when JS is off, Features scroll inside their view
		"overflow-x": "hidden",
		"overflow-y": "hidden"
	});
	
	$("a.current").click(function(){
		return false;
	});
	$("a.current").each(function(){
		return false;
	});
	
	
	
	
	
	
	
	// ######################################### MENU MOUSEOVER #######################################################
	$("#menu li").hover(function(){
		$(this).addClass("selected");
		Cufon.replace("#menu .selected a", { fontFamily: 'Anivers' }); // recufonize texts
	}, function(){
		$("#menu li").removeClass("selected");
		Cufon.replace("#menu a", { fontFamily: 'Anivers' }); // recufonize texts
	});
	
	
	
	// ######################################### TOOLTIPS #######################################################
	$("a").not('a.perma').each(function(i){
	 	$("body").append("<div class='tooltip' id='tooltip"+i+"'><p>"+$(this).attr('title')+"</p></div>");
	 	var my_tooltip = $("#tooltip"+i);
	 	if($(this).attr("title") != ""){ // checks if there is a title
	 		$(this).removeAttr("title").mouseover(function(){
	 				my_tooltip.css({opacity:0.9, display:"none"}).fadeIn(400);
					//my_tooltip.css({opacity:0.9, display:"none"});
	 		}).mousemove(function(kmouse){
	 				my_tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
	 		}).mouseout(function(){
	 				my_tooltip.fadeOut(400);
	 		});
	 	}
	 });
	     
		 
		 
	// ######################################### TOP DRAWER #######################################################
	
	// Set different heights for the drawer based on the browser
	if($.browser.msie){
		if(parseInt($.browser.version) == 6){
			window.drawerHeight = 201;
			btov_drawer = "-20px";
			btov_cuform = "-100px";
		} else if(parseInt($.browser.version) == 8){
			window.drawerHeight = 159;
			btov_drawer = "-120px";
			btov_cuform = "-100px";
		} else if(parseInt($.browser.version) == 7){
			window.drawerHeight = 159;
			btov_drawer = "-20px";
			btov_cuform = "-100px";
		} else {
			window.drawerHeight = 179;
		}
	} else {
		window.drawerHeight = 159;
		btov_drawer = "-120px";
		btov_cuform = "-100px";
	}
	
	
	// Control drop dow of top drawer
	
	var currentDraw = "";
	
	$("#hire-me-drawer").css({"top": "-"+window.drawerHeight+"px", "display": "block"});//display it
	
	function resetDrawButtons(){
		$("#drawer-button-call, #drawer-button-login").attr('class','drawButton');
		$("#drawer-button").attr('class','drawEndButton');
	}
	
	$("#drawer-button").click(function(){
		//alert(currentDraw);
		$(this).attr('class','drawEndButton');
		$("#drawer-button-call, #drawer-button-login").attr('class','drawButton-inactive');
		if(parseInt($("#hire-me-drawer").css("top")) == 0){
			//alert("css:0");
			if(currentDraw == "log"){
				$('.drawer-main').cycle(0);
				$(this).delay(1500,function(){
					$('div.drawer-details').cycle(0);
				});
				$(this).html("Close");
				$("#drawer-button-login").html("Client Login");
				$("#drawer-button-call").html("Call Me Back!");
				currentDraw = "enq";
			} else if(currentDraw == "cal"){
				$('.drawer-main').cycle(0);
				$(this).delay(1500,function(){
					$('div.drawer-details').cycle(0);
				});
				$(this).html("Close");
				$("#drawer-button-login").html("Client Login");
				$("#drawer-button-call").html("Call Me Back!");
				currentDraw = "enq";
			} else if(currentDraw == "enq"){
				resetDrawButtons();
				$("#hire-me-drawer").animate({"top": "-"+window.drawerHeight+"px"}, 1200, "easeOutQuart");
				$(this).html("Enquire Now");
				currentDraw = "";
				$('form#drawer-enqForm, form#drawer-login, form#drawer-cmbForm').clearForm();
			}
		} else {
			//alert("Dropping");
			$('.drawer-main').cycle({startingSlide: 0, fx: "scrollHorz", timeout: 0, cleartype: true, cleartypeNoBg: true, easing: "easeOutQuart", speed: 1000});
			$("#hire-me-drawer").animate({"top": "0px"}, 1000, "easeOutBounce");
			$(this).html("Close");
			currentDraw = "enq";
		}
	}).focus(function(){
		$(this).blur();// this removes focus border that looked awful
	});
	
	$("#drawer-button-login").click(function(){	
		//alert(currentDraw);
		$(this).attr('class','drawButton');
		$("#drawer-button-call").attr('class','drawButton-inactive');
		$("#drawer-button").attr('class','drawEndButton-inactive');
		if(parseInt($("#hire-me-drawer").css("top")) == 0){
			if(currentDraw == "enq"){
				$('.drawer-main').cycle(1);
				$(this).html("Close");
				$("#drawer-button").html("Enquire Now");
				$("#drawer-button-call").html("Call Me Back!");
				currentDraw = "log";
			} else if(currentDraw == "cal"){
				$('.drawer-main').cycle(1);
				$(this).html("Close");
				$("#drawer-button").html("Enquire Now");
				$("#drawer-button-call").html("Call Me Back!");
				currentDraw = "log";
			} else if(currentDraw == "log"){
				//##### THIS DRAWER OPEN SO CLOSE IT #####
				resetDrawButtons();
				$("#hire-me-drawer").animate({"top": "-"+window.drawerHeight+"px"}, 1200, "easeOutQuart");
				$(this).delay(1500,function(){
					$('div.drawer-details').cycle(0);
				});
				$(this).html("Client Login");
				currentDraw = "";
				$('form#drawer-enqForm, form#drawer-login, form#drawer-cmbForm').clearForm();
			}
		} else {
			$('.drawer-main').cycle({startingSlide: 1, fx: "scrollHorz", timeout: 0, cleartype: true, cleartypeNoBg: true, easing: "easeOutQuart", speed: 1000});
			$("#hire-me-drawer").animate({"top": "0px"}, 1000, "easeOutBounce");
			$(this).html("Close");
			currentDraw = "log";
		}
	}).focus(function(){
		$(this).blur();// this removes focus border that looked awful
	});
	
	
	$("#drawer-button-call").click(function(){	
		//alert(currentDraw);
		$("#radCallback_asap").attr("checked", true);
		$(this).attr('class','drawButton');
		$("#drawer-button-login").attr('class','drawButton-inactive');
		$("#drawer-button").attr('class','drawEndButton-inactive');
		if(parseInt($("#hire-me-drawer").css("top")) == 0){
			if(currentDraw == "enq"){
				$('.drawer-main').cycle(2);
				$(this).html("Close");
				$("#drawer-button").html("Enquire Now");
				$("#drawer-button-login").html("Client Login");
				currentDraw = "cal";
			} else if(currentDraw == "log"){
				$('.drawer-main').cycle(2);
				$(this).html("Close");
				$("#drawer-button").html("Enquire Now");
				$("#drawer-button-login").html("Client Login");
				currentDraw = "cal";
			} else if(currentDraw == "cal"){
				resetDrawButtons();
				$("#hire-me-drawer").animate({"top": "-"+window.drawerHeight+"px"}, 1200, "easeOutQuart");
				$(this).delay(1500,function(){
					$('div.drawer-details').cycle(0);
				});
				$(this).html("Call Me Back!");
				currentDraw = "";
				$('form#drawer-enqForm, form#drawer-login, form#drawer-cmbForm').clearForm();
			}
		} else {
			$('.drawer-main').cycle({startingSlide: 2, fx: "scrollHorz", timeout: 0, cleartype: true, cleartypeNoBg: true, easing: "easeOutQuart", speed: 1000});
			$("#hire-me-drawer").animate({"top": "0px"}, 1000, "easeOutBounce");
			$(this).html("Close");
			currentDraw = "cal";
		}
	}).focus(function(){
		$(this).blur();// this removes focus border that looked awful
	});
	
	
	$("#drawer-button-logout").click(function(){	
		window.location = "http://www.tangent360.com/includes/logout.php";
	});
	

	// Control sliding divs in top drawer
	
	$('.drawer-main').cycle({
		fx: "scrollHorz",
		timeout: 0,
		cleartype:  true,
    	cleartypeNoBg:  true,
		easing: "easeOutQuart",
		speed: 1000,
		startingSlide: 0
	});
	
	// Control sliding divs in enq form
	
	$('form#drawer-enqForm').cycle({
		fx: "scrollVert",
		timeout: 0,
		cleartype:  true,
    	cleartypeNoBg:  true,
		easing: "easeOutQuart",
		speed: 500
	});
	
	// Control sliding divs in client login
	
	$('#clogForm').cycle({
		fx: "scrollVert",
		timeout: 0,
		cleartype:  true,
    	cleartypeNoBg:  true,
		easing: "easeOutQuart",
		speed: 500
	});
	
	// Control sliding divs in cmb form
	
	$('#cmbForm').cycle({
		fx: "scrollVert",
		timeout: 0,
		cleartype:  true,
    	cleartypeNoBg:  true,
		easing: "easeOutQuart",
		speed: 500
	});
	
	// Control sliding divs in details
	
	$('div.drawer-details').cycle({
		fx: "scrollVert",
		timeout: 0,
		cleartype:  true,
    	cleartypeNoBg:  true,
		easing: "easeOutQuart",
		speed: 900,
		startingSlide: 0
	});
	
	$('span#link1').click(function(){
		$('div.drawer-details').cycle(1);
	});
	$('span#link2').click(function(){
		$('div.drawer-details').cycle(0);
	});
	$('span#link3').click(function(){
		$('div.drawer-details').cycle(1);
	});
	
	
	
	// ######################################### CROSSFADES FOR SERVICE ICONS #######################################################
	
	$(".service-icon").each(function (i, element) {
		var src = $(element).find("img").attr("src").replace(".png", "-hover.png");
		$(element).css({background: "url("+src+") no-repeat 1px 1px"});
		$(element).hover(function(){
			$(this).find("img").animate({opacity: 0}, 700);
		}, function(){
			if($(this).find("img:animated").size() == 0){
				$(this).find("img").animate({opacity: 1}, 700);
			} else {
				$(this).find("img").stop().animate({opacity: 1}, 300);
			}
		});
	});
	
	
	// ######################################### CROSSFADES FOR SERVICE BUTTONS #######################################################
	
	// main short buttons
	$(".main-button-short, .formButton, .cuFormButton").each(function(i, element){
		if($(this).attr("class") == "cuFormButton"){
			theOffset = btov_cuform;
		} else {
			theOffset = btov_drawer;
		}
		var hover = $(element).clone().css({
			"background": "url(" + rootUrl + "images/main-button-short-blue.jpg) no-repeat",
			"position": "relative", 
			"left": theOffset, 
			"z-index": "5", 
			"opacity": "0", 
			"margin-right": "-120px",
			"color": "#2f3738"
		});
		$(element).after(hover);
		$(element).next().hover(function(){
			$(this).animate({opacity: 1}, 240);
		}, function(){
			if($(this).is(":animated")){
				$(this).stop().animate({opacity: 0}, 100);
			} else {
				$(this).animate({opacity: 0}, 240);
			}
		});
	});
	
	
	// buttons in features
	$(".feature-buttons a").each(function(i, element){
		var hover = $(element).clone().css({
			"background": "url(" + rootUrl + "images/feature-button-dark.jpg) no-repeat",
			"position": "relative", 
			"left": "-120px", 
			"z-index": "5", 
			"opacity": "0", 
			"margin-right": "-120px", 
			"color": "#4ad8e6"
		});
		if($(element).hasClass("feature-button-next")){
			hover.css({"margin-right": "-100px", "left": "-100px"});
		}
		$(element).after(hover);
		$(element).next().hover(function(){
			$(this).animate({opacity: 1}, 240);
		}, function(){
			if($(this).is(":animated")){
				$(this).stop().animate({opacity: 0}, 100);
			} else {
				$(this).animate({opacity: 0}, 240);
			}
		});
	});
	
	// long buttons in main
	$(".main-button-long, .main-button-long-port").each(function(i, element){
		if($(this).attr("class") == "main-button-long"){
			var topper = "-65px";
		} else if($(this).attr("class") == "main-button-long-port"){
			var topper = "-50px";
		}
		var hover = $(element).clone().css({
			"background": "url(" + rootUrl + "images/main-button-long-blue.jpg) no-repeat",
			"position": "relative", 
			"top": topper, 
			"z-index": "5", 
			"opacity": "0", 
			"margin-bottom": "-65px",
			"color": "#2f3738"
		});
		
		$(element).after(hover);
		$(element).next().hover(function(){
			$(this).animate({opacity: 1}, 300);
		}, function(){
			if($(this).is(":animated")){
				$(this).stop().animate({opacity: 0}, 100);
			} else {
				$(this).animate({opacity: 0}, 300);
			}
		});
	});
	
	// long buttons in main
	$(".searchBtn").each(function(i, element){
		/*if($(this).attr("class") == "main-button-long"){
			var topper = "-65px";
		} else if($(this).attr("class") == "main-button-long-port"){
			var topper = "-50px";
		}*/
		var hover = $(element).clone().css({
			"background": "url(" + rootUrl + "images/blogSearch_dark.png) center 2px no-repeat",
			"position": "relative", 
			"top": "-29px",
			/*"left": "74px",*/
			"z-index": "5", 
			"opacity": "0", 
			"margin-top": "29px",
			"color": "#75e1eb",
			"float": "right",
			"width": "74px",
			"height": "28px",
			"margin-right": "-74px"
		});
		
		$(element).after(hover);
		$(element).next().hover(function(){
			$(this).animate({opacity: 1}, 300);
		}, function(){
			if($(this).is(":animated")){
				$(this).stop().animate({opacity: 0}, 100);
			} else {
				$(this).animate({opacity: 0}, 300);
			}
		});
	});
	
	
	
	
	
	// ######################################### MAIN FEATURES CYCLER #######################################################
	
	
	/*function do_err(){
        return true
    }
	onerror=do_err;
	function no_cp(){
        clipboardData.clearData();
		setTimeout("no_cp()",100);
    }
	no_cp();*/
	
	  
	$("img.featureImg, img.iconned, img.img-float-left-wide, img.img-float-left-narrow, img.largeIcon, img.navup, img.img-serv-med, img.img-fullMain, img.blogFade").hover(function() {
		$(this).animate({backgroundColor: '#181c1c'}, "slow");
		}, function() {
			if($(this).is(":animated")){
				$(this).stop().animate({backgroundColor: "#252f31"}, "normal");
			} else {
				$(this).animate({backgroundColor: "#252f31"}, "normal");
			}	
	});
	
	$("div.proSection").hover(function() {
		$("img", this).animate({backgroundColor: '#181c1c'}, "slow");
		}, function() {
			if($("img", this).is(":animated")){
				$("img", this).stop().animate({backgroundColor: "#252f31"}, "normal");
			} else {
				$("img", this).animate({backgroundColor: "#252f31"}, "normal");
			}	
	});
	
	$("div.sectorGrip").hover(function() {
		$("img", this).animate({backgroundColor: '#181c1c'}, "slow");
		}, function() {
			if($("img", this).is(":animated")){
				$("img", this).stop().animate({backgroundColor: "#252f31"}, "normal");
			} else {
				$("img", this).animate({backgroundColor: "#252f31"}, "normal");
			}	
	}).click(function() {
		var destUrl	= $(this).attr('id').replace("-", "#");
		window.location = destUrl;
	});
	
	
	/*
	$("div.sectorGrip").hover(function() {
		$(this).siblings().each(function(i, element){
				if($(element).is(":animated")){
					$(element).stop().animate({backgroundColor: "#2f3738", borderTopColor: "#2f3738", borderLeftColor: "#2f3738", borderBottomColor: "#2f3738", borderRightColor: "#2f3738"}, 10);
					$("img", element).stop().animate({backgroundColor: '#252f31', borderTopColor: "#4d5859", borderLeftColor: "#4d5859", borderBottomColor: "#4d5859", borderRightColor: "#4d5859"}, 10);
				}
		});
		$(this).animate({backgroundColor: '#181c1c', borderTopColor: "#4d5859", borderLeftColor: "#4d5859", borderBottomColor: "#4d5859", borderRightColor: "#4d5859"}, "slow");
		$("img", this).animate({backgroundColor: '#181c1c', borderTopColor: "#181c1c", borderLeftColor: "#181c1c", borderBottomColor: "#181c1c", borderRightColor: "#181c1c"}, "slow");
		}, function() {
			if($(this).is(":animated")){
				$(this).stop().animate({backgroundColor: "#2f3738", borderTopColor: "#2f3738", borderLeftColor: "#2f3738", borderBottomColor: "#2f3738", borderRightColor: "#2f3738"}, "fast");
				$("img", this).stop().animate({backgroundColor: '#252f31', borderTopColor: "#4d5859", borderLeftColor: "#4d5859", borderBottomColor: "#4d5859", borderRightColor: "#4d5859"}, "fast");
			} else {
				$(this).animate({backgroundColor: "#2f3738", borderTopColor: "#2f3738", borderLeftColor: "#2f3738", borderBottomColor: "#2f3738", borderRightColor: "#2f3738"}, "fast");
				$("img", this).animate({backgroundColor: '#252f31', borderTopColor: "#4d5859", borderLeftColor: "#4d5859", borderBottomColor: "#4d5859", borderRightColor: "#4d5859"}, "fast");
			}	
	}).click(function() {
		var thid = $(this).attr("id");
		window.location = thid;
	});
	*/

		
		
});

