var settings = {dragMinHeight:60,dragMaxHeight:60, topCapHeight:0, bottomCapHeight:20, scrollbarWidth:30};

$(document).ready(function(){	
	init();
	//setTimeout("debug()",5000);
});

function init(){
	if (!$("#divHome").length) {
		$("#wrapper #container #header").css("width",$("#wrapper #container #header").width()).addClass("fixed");
		$("#wrapper #container .sidebar").addClass("fixed");;
	}
	
	$("#imgBg").loadBg();
	
	$.preloadImages("../images/bgmenu.png","../images/bghover.png", "../images/wearebossa-black.png");

	$.extend($.validator.messages, {
		required: "Fill the required field"
	});
		
	$('a[rel="_blank"],a.-blank').click(function(e) {
		this.target = "_blank";
	});

	if ($("#divLanguage").length && !jQuery.browser.msie) {
		$("#divLanguage a").each(function(index) {
			if ($(this).attr("href").indexOf("br")!=-1) {
				$(this).attr("title","Vers&#227;o em Portugu&#234;s");
				img = $(this).find("img");
				img.replaceWith($("<canvas class='CoolClock:simple:::-3 leftRightPad'></canvas>"));
			}
			else {
				$(this).attr("title","English version");
				img = $(this).find("img");
				img.replaceWith($("<canvas class='CoolClock:simple:::+1 leftRightPad'></canvas>"));
			}
		});
		CoolClock.findAndCreateClocks();
		$('.language-link').click(function(e) {
			if ($(this).hasClass("active")) {
				e.preventDefault();
			}
		});
	}
	
	if (!jQuery.browser.msie || (jQuery.browser.msie && (parseInt(jQuery.browser.version) > 7))) {
		$("div.-overall a:not(.active)").hover(function(){
			$("div#shadow").toggle();
			$("div#divMenuBottom a.wearebossa").addClass("active");
		},function(){
			$("div#shadow").toggle();
			$("div#divMenuBottom a.wearebossa").removeClass("active");
		});		
	}
	
	if ($("a.-mail").length){
		$("a.-mail").each(function() {	
			$(this).enableMail();
		});
	}
	
	if ($("#divForm .hideLabel").length) {
		$("#divForm .hideLabel").each(function() {	
			$(this).hideLabels();
		});
	}
	
	if ($("#divForm").length){
		$('#divForm textarea').keyup(function() {
			var len = this.value.length;
			if (len >= 350) {
				this.value = this.value.substring(0, 350);
			}
		});
				
		if ($(".captcha-wrapper")) {
			$(".captcha-wrapper a").click(function (e) {
				e.preventDefault();
				var img = $(".captcha-wrapper").find("img");
				img.attr("src","http://www.martinscosta.com/sites/martinscosta.com/themes/martinscosta/captcha/mccaptcha.php?f="+$("input[name=formname]").val()+"&t="+new Date().getTime());
			});
		}
		$("#divForm").find("input:submit").removeAttr('disabled');
		$("#divForm form").validate({
			event: "change",
			rules: {
				name: {noLabelTxt: true},
				email: {noLabelTxt: true, email: true,equalTo: "#cemail"},
				cemail: {equalTo: "#email"},
				message: {noLabelTxt: true},
				captcha: {required: true, remote: "http://www.martinscosta.com/sites/martinscosta.com/themes/martinscosta/captcha/verify.php?f="+$("input[name=formname]").val()}
			},
			messages: {
				name: "Please enter your name",
				email: "Please enter a valid email address",
				cemail: "Please enter the same email",
				message: "Please write a message",
				captcha: "Please enter the number"
			},
			errorPlacement: function(error, element) {
				error.appendTo(element.parent().parent().next().empty());
			},
			submitHandler: function(form) {
				$("#divForm").find("input:text,textarea").each(function() {	
					if ($(this).val() == $(this).parent().parent().find("label").html()) $(this).val("");
				});
				$("#divForm").find("input:submit").attr('disabled', 'disabled').val("Sending...");
				
				form.submit();
			},
			success: function(label) {
				if (!label.hasClass("checked")) {
					label.html("&nbsp;").removeClass("error").addClass("checked");
				}
			}
		});
	}
	
	$("#wrapper #container .sidebar").height($(window).height());
	
	$(window).resize(function() {
		$("#wrapper #container .sidebar").height($(window).height());
		if ($(window).width()>1150) {
			$("#wrapper #container #header").width(1150);
		}
		else
		if ($(window).width()>1024) {
			$("#wrapper #container #header").width($(window).width());
		}
	});

	
	//setDimensions();	
}

function setDimensions(){
	//$("#container").height($(document).height());
	if (!jQuery.browser.msie || (jQuery.browser.msie && (parseInt(jQuery.browser.version) > 6))) {		
		showScroll();
	}
	/*
	$(window).resize(function() {
		$("#container").height($(document).height());		
	});
	*/
}

function showScroll(){
	$(".txtwrapper").jScrollPane(settings);
	$(".jScrollPaneTrack").appendTo($("#sidebar-right"));
	/*
	if (($(".txtcontent").height() > $("#center").height()-140)) {
		$(".txtwrapper").css("overflow","hidden").jScrollPane(settings);
		if (!$("#divScroll").length) {
			var max = $(".txtcontent").height()-$(".txtwrapper").height();
			var scroll = $('<div id="divScroll"></div>');
						
			scroll.slider({
				orientation: "vertical",
				range: "min",
				min: 0,
				max: max,
				value: max,
				slide: function(event, ui) {
					$(".txtcontent").css("top",-(max-ui.value)+"px");
				}
			});
			scroll.height($("#center").height()-230);
			$("#sidebar-right").append(scroll);
			$(".txtcontent").css("top",(max-scroll.slider("value"))+"px");
		}
	}
	else {
		$("#sidebar-right").empty();
	}
	*/
}





function debug(){
	var head = document.getElementsByTagName("head")[0];         
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = 'https://getfirebug.com/firebug-lite.js';
	head.appendChild(script);
}

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}

jQuery.fn.switchimage = function() {
	var _this = $(this);
	setTimeout(function(){alert(1);},3000);
	$(this).css("opacity",0).attr("src",$(this).attr("src")).load(function() {
		$(this).animate({opacity:1},"fast");
	});
}

jQuery.fn.loadBg = function() {
	$.fn.supersized.options = {  
		startwidth: 640,  
		startheight: 480,
		vertical_center: 1,
		slideshow: 1,
		navigation: 1,
		transition: 1, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
		pause_hover: 0,
		slide_counter: 1,
		slide_captions: 1,
		slide_interval: 3000  
	};
	$(this).supersized(); 
}

jQuery.fn.scrollUp = function() {
	var top = Math.abs(parseInt($(".txtcontent").css("top").replace("px","")));
	var seconds = top*3;
	if (parseInt($(".txtcontent").css("top").replace("px",""))<0) {
		$(".txtcontent").animate({top:0},{duration:seconds,easing:'linear'});
	}
}

jQuery.fn.scrollDown = function() {
	var top = Math.abs(parseInt($(".txtcontent").css("top").replace("px","")));
	var seconds = ($(".txtcontent").height()-$(".txtwrapper").height()-top)*3;
	if (top < ($(".txtcontent").height()-$(".txtwrapper").height())) {
		$(".txtcontent").animate({top:"-"+($(".txtcontent").height()-$(".txtwrapper").height())},{duration:seconds,easing:'linear'});
	}
}

jQuery.fn.enableMail = function() {
	$(this).attr("href",$(this).attr("href").replace("[a]","@"));
	$(this).html($(this).html().replace("[a]","@"));
}

jQuery.fn.hideLabels = function() {
	var input = $(this);
	var label = input.parent().parent().find("label");
	label.hide();
	input.val(label.html());
	input.focus(function(){
		if (input.val() == label.html()) input.val("");
	}).blur(function(){
		if (input.val() == "") input.val(label.html());
	});
}

jQuery.validator.addMethod("noLabelTxt", function(value, element) {
	var label = $(element).parent().parent().find("label");
    return this.optional(element) || (value != label.html());
});

