$(document).ready(function() {
   	init();
});

function init(){

	/* Show textarea
	-------------------------------------------------- */
	if ($('#message')) {
		$('#message').hide();
		$('a.messagelink').click(function(){
			$('#message').show();
			$('a.messagelink').hide();

			return false;
		}); 
	}
	
	/* Show tooltip
	-------------------------------------------------- */
	if ($('.work span.left')) {
		$('.work span.left').bind("mouseenter",function(){
			$('.tooltip').show();
		});
	}
	
	if ($('.work span.left')) {
		$('.work span.left').bind("mouseleave",function(){
			$('.tooltip').hide();
		});
	}

	/* Go to case page and activate clicked project
	-------------------------------------------------- */
	$(".index .project a").click(function() {
		this_id = $(this).attr("id");
		id = this_id.substr(8);

		window.location = "http://www.britny.se/case/?projekt="+id;
		return false;
	}); 		

	/* Clear default value in input
	------------------------------------- */
	$("input[type=text], textarea").focus(function() { 
		txt = $(this).val();
		if(txt == 'För & efternamn' || txt == 'E-mail' || txt == 'Företag' || txt == 'Meddelande') {
			$(this).val("");
		}
	});
	
	
	/* Style file input
	------------------------------------- */
	$(".contact input[type=file]").filestyle({ 
    	image: "http://www.britny.se/images/btn-browse.gif",
     	imageheight : 24,
     	imagewidth : 121,
     	width : 176
	});
	
	$(".about input[type=file]").filestyle({ 
    	image: "http://www.britny.se/images/btn-browse.gif",
     	imageheight : 24,
     	imagewidth : 121,
     	width : 220
	});
	
	
	/* Contact-page
	------------------------------------- */
	
	$(".tictic .thisisbritny").hide();
	
	$(".tictic .head a").click(function() { 
		$(".tictic .thisisbritny").toggle('fast');
		$(".tictic").toggleClass("full");
		targetOffset = $(".thisisbritny").offset().top;
		$('html,body').animate({scrollTop: targetOffset}, 500);
		
		return false;
	});
	
	
	/* Work form
	------------------------------------- */
	new Ajax_upload('#filebutton', {
		  action: '../script/php/upload.php',
		  name: 'myfile',
		  data: {
		    type : 'filebutton'
		  },
		  autoSubmit: true,
		  onSubmit: function(file, extension) {
		  	$('#response').show();
			$("#response").html('<img src="../images/loader20x20.gif" alt="" /> <span>v&auml;nta...</span>');
			$("#response").addClass("loader");
		  },
		  onComplete: function(file, response) {
		  	$("#response").html(response);
			if(response == 'Filen laddades upp') {
				$('.file').val(file);
				$("#lnk").val(file);
				$("#response").removeClass("loader");
			}
		  }
		});
		
	$('#sendbtn').click(function() {
		lnk = $('#lnk').val();
		str = $("input[name='subject']:checked").val();
		message = $("#message").val();
		if (lnk != "") {
			jQuery(function($){
				$("#ajaxcontent").load("http://www.britny.se/script/php/sendmail.php", {
					lnk: lnk,
					str: str,
					message: message
				});
			});
		}else {
			$('#response').show();
			$("#response").html('<span>Ingen fil vald</span>');
		}
		return false;
	});
	
	
	/* Partners
	------------------------------------- */
	/* Swap image on mouseenter */
	$('.partnerrow .logo a img').bind("mouseenter",function(){
		image_src = $(this).attr("src");
		image = image_src.substr(36);
		active_image = "http://www.britny.se/files/partners/active-" + image;
		image_class = $(this).attr("class");
		
		if (image_class != 'active') {
		
			$(this).attr({ 
        	  src: active_image
        	});
        
        }
	});
	
	/* Swap image on mouseleave */
	$('.partnerrow .logo a img').bind("mouseleave",function(){
		image_src = $(this).attr("src");
		image = image_src.substr(43);
		deafult_image = "http://www.britny.se/files/partners/" + image;
		image_class = $(this).attr("class");
		
		if (image_class != 'active') {
			$(this).attr({ 
        	  src: deafult_image
        	});
        }
	});
	
	/* View description */
	$('.partnerrow .logo a').click(function() {
		id = $(this).attr('id');
		row = $(this).parent().parent().attr('id');
		
		this_class = $(this).children().attr("class");
		
		if (this_class == "active") {
			$("#" + row + " div.partnerdesc").remove();
			old_image_src = $(this).children().attr('src');
			$(this).children().removeClass('active');
			if(old_image_src) {
				image = old_image_src.substr(36);
				$(this).children().attr('src', 'http://www.britny.se/files/partners/' + image);
			}
		}
		else {
			jQuery(function($){
				$("#" + row + " div.partnerdesc").remove();
				$('<div class="partnerdesc"></div>').appendTo("#" + row).load("../script/php/functions.php", {
					id: id,
					todo: 'get_active_partner'
				});
			});
			old_image_src = $('#'+row+' .logo a img.active').attr('src');
			if(old_image_src) {
				image = old_image_src.substr(43);
				$('#'+row+' .logo a img.active').attr('src', 'http://www.britny.se/files/partners/' + image);
			}else {
				/* SCROLLING */
				targetOffset = $("#"+row).offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
			}
			$('#'+row+' .logo a img.active').removeClass('active');
			$(this).children().addClass("active");
		}
		return false;
	}); 
	
	
	/* Case
	------------------------------------- */
	$('#casenav a').click(function() {
		id = $(this).attr('id');
		$('#casenav a').removeClass('activecase');
		$(this).addClass("activecase");
		height = $(".casedesc").css('height');
		$(".casedesc").html('<div class="loader" style="height: '+height+';"><img src="../images/loader50x54.gif" alt="Laddar" /></div>');
		jQuery(function($) {
			$('.casedesc').load("../script/php/functions.php",{id: id, todo: 'get_active_case'});
		});
		
		/* Run sIFR when content is loaded */		
		$(".casedesc").ajaxComplete(function(request, settings) {
		
			var avantgardebold = { src: '/flash/sifr/avantgardebold.swf' };
			var freehand = { src: '/flash/sifr/freehand.swf' };
			
			sIFR.activate(avantgardebold, freehand);
			sIFR.replace(avantgardebold, {
			  selector: '.cases h1',
			  wmode: 'transparent',
			  tuneHeight: -2,
			  css: [
			    '.sIFR-root { color: #404040;}'
			  ]
			})
			
			sIFR.replace(freehand, {
			  selector: 'blockquote',
			  wmode: 'transparent',
			  tuneHeight: -2,
			  css: [
			    '.sIFR-root { text-align: center; color: #707070; leading: -8; }'
			  ]
			})
    	});
		return false;
	});
	
	/* Projects
	------------------------------------- */
	// Projects on startpage
	/* Swap image on mouseleave */
	$('.index .projectrow .logotype a img').bind("mouseenter",function(){
        image_src = $(this).attr("src");
		image = image_src.substr(48);
		
		deafult_image = "http://www.britny.se/files/projects/image_" + image;
		image_class = $(this).attr("class");
		this_parent = $(this).parent().attr('id');
		
		if (image_class != 'active') {
			$("#"+this_parent+" .icon").show();
			$(this).attr({ 
        	  src: deafult_image
        	});
        }
	});
	
	/* Swap image on mouseleave */
	$('.index .projectrow .logotype a img').bind("mouseleave",function(){
		image_src = $(this).attr("src");
		image = image_src.substr(42);
		active_image = "http://www.britny.se/files/projects/image_hover_" + image;
		image_class = $(this).attr("class");
		this_parent = $(this).parent().attr('id');
		
		if (image_class != 'active') {
			$(this).attr({ 
        	  src: active_image
        	});
        }
	});
	
	/* Swap image on mouseenter */
	$('.projects .projectrow .logotype a img').bind("mouseenter",function(){
		image_src = $(this).attr("src");
		image = image_src.substr(42);
		
		active_image = "http://www.britny.se/files/projects/image_hover_" + image;
		image_class = $(this).attr("class");
		this_parent = $(this).parent().attr('id');
		
		if (image_class != 'active') {
			$(this).attr({ 
        	  src: active_image
        	});
        }
	});
	
	/* Swap image on mouseleave */
	$('.projects .projectrow .logotype a img').bind("mouseleave",function(){
		image_src = $(this).attr("src");
		image = image_src.substr(48);
		deafult_image = "http://www.britny.se/files/projects/image_" + image;
		image_class = $(this).attr("class");
		
		if (image_class != 'active') {
			$("#"+this_parent+" .icon").show();
			$(this).attr({ 
        	  src: deafult_image
        	});
        }
	});
	
	current_page = window.location;
	
	d = current_page.toString();
	
	p = d.substr(35);
	
	if (p) {
	    id = "projekt-"+p;	    
	    row = "projectrow-1";
	    this_parent = "project-"+p;
	    image_active = true;
	    show_active_project(id, row, this_parent, image_active);
	}
		
	$('.projects .projectrow .logotype a').click(function() {
	    id = $(this).attr('id');
	    row = $(this).parent().parent().parent().attr('id');
	    this_parent = id;
	    image_active = false;
	    show_active_project(id, row, this_parent, image_active);
	    return false;
	 });
	
	
	/* Coworkers
	------------------------------------- */
	$("#coworkers .person").bind("mouseenter",function() {
		id = $(this).attr("id");
		$(this).children().hide();
		$(this).addClass('active');
		
		name = $(this).children().attr('alt');
		person_status = $(".active .caption").text();
		$(this).append('<span class="personinfo"><span class="name">'+name+'</span><span class="status">'+person_status+'</span></span>');
		
		// $('<span class="personinfo"></span>').appendTo(this).load("../script/php/functions.php", {id: id, todo: 'get_active_person'});
	});
	
	$("#coworkers .person").bind("mouseleave",function() {
		$(this).children().show();
		$(this).removeClass('active');
	});
}

/* Show clicked project from startpage
------------------------------------- */
function show_active_project(id, row, this_parent, image_active) {
	
	if (image_active) {
		//$("#"+id).children().addClass("active");
		old_image_src2 =$("#"+id).children().attr('src');
		image2 = old_image_src2.substr(42);
	   $("#"+id).children().attr('src', 'http://www.britny.se/files/projects/image_hover_' + image2);
	   $("#"+row+" .project .logotype a .icon").hide();
	}

	if($("#"+id).parent().parent().attr('class') == 'project active' || $("#"+id).parent().parent().attr('class') == 'project last active') {
	    $("#"+row+" div.showproject").remove();
	    $("#"+id).parent().parent().removeClass('active');
	    
	    old_image_src = $("#"+id).children().attr('src');
	    if(old_image_src) {
	    	image = old_image_src.substr(42);
	    	this_id = $(this).attr('id');
	    	$("#"+this_id+ " img").removeClass('active');
	    	$("#"+this_id+ " img").attr('src', 'http://www.britny.se/files/projects/image_' + image);
	    }
	    $("#"+this_parent+" .icon").removeClass('active');
	    $("#"+this_parent+" .icon").show();
	}else {
	    old_image_src2 = $('#'+row+' img.active').attr('src');
	    
	    if(old_image_src2) {
	    	
	    	image2 = old_image_src2.substr(48);
	    	$('#'+row+' img.active').attr('src', 'http://www.britny.se/files/projects/image_' + image2);
	    	$("#"+row+" .project .logotype a .icon").removeClass('active');
	    	$("#"+row+" .project .logotype a .icon").show();
	    }
	    $('#'+row+' img').removeClass('active');
	    $('#'+row+' .project').removeClass('active');
	    $("#"+id).parent().parent().addClass('active');
	    jQuery(function($) {
	    	
	    	$("#"+this_parent+" .icon").hide();
	    	$("#"+row+" div.showproject").remove();
	    	$('<div class="showproject"></div>').appendTo("#"+row).load("../script/php/functions.php",{id: id, todo: 'get_active_project'}, function() {
	    		$("a.closedesc").click(function() {
	    			row = $("#"+id).parent().parent().parent().attr('id');
	    			$(this).parent().parent().remove();
	    			$('#'+row+' .project').removeClass('active');
	    			old_image_src2 = $('#'+row+' img.active').attr('src');
	    
	    			if(old_image_src2) {
	    				image2 = old_image_src2.substr(48);
	    				
	    				$('#'+row+' img.active').attr('src', 'http://www.britny.se/files/projects/image_' + image2);
	    			}
	    			$('#'+row+' img').removeClass('active');
	    			$("#"+row+" .project .logotype a .icon").removeClass('active');
	    			$("#"+row+" .project .logotype a .icon").show();
	    			return false;
	    		});
	    	}); 
	    	/* SCROLLING */
	    	targetOffset = $("#"+row).offset().top;
	    	$('html,body').animate({scrollTop: targetOffset}, 1000);
	    });
	    $("#"+id).children().addClass("active");
	    
	}
}