


$(document).ready(function(){

		 setTimeout(function(){
  $(".messages").fadeOut("slow", function () {
  $(".messages").remove();
      });

}, 2000);

		$('.block-mailchimp h3').click(function() {
					newsletter();
	});
		

	window.status = 'Scandinavian DesignLab © All Rights Reserved 2010';
	/* FRONTPAGE SLIDESHOW */
	if($('#wallbody').length > 0) {
		setInterval('frontSlideShow()', 3500);
	}
	/* ABOUT PAGE ANIMATED SCROLL */
	$('.about_menu').click(function(){
		var id = $(this).attr('title');
		var px = $('#'+id).offset().top - 20;
		$('html, body').animate({scrollTop: px}, 500, 'swing');
		return false;
	});
	/* CASE FULL VIEW */
	if ($("#case_elements").length > 0){
		$(window).bind('resize', resizeCase);
		$(window).load(resizeCase);
	}
	/* CASES HOVERING EFFECT */
	$('.case_images').click(function(){
		location.href = $(this).attr("title");
	});
	$('.case_images div:first-child').each(function(){
		var image = $(this).children('img').attr('src');
		$(this).parent().css('background-image','url('+image+')');
	});
	$('.case_images div img').css('visibility','hidden');
	$('.case_images div').each(function(){
		$(this).css('width','45px');
	});
	$('.case_images div:nth-child(2)').css('margin-left','45px');
	$('.case_images div:nth-child(3)').css('margin-left','90px');
	$('.case_images div:nth-child(4)').css('margin-left','135px');
	$('.case_images div').hover(function(){
		var image = $(this).children('img').attr('src');
		$(this).parent().css('background-image','url('+image+')');
	});
	/* NEWSLETTER SIGNUP FORM */
	$('.sdl_navigation_footer').click(function(){
		$('.sdl_navigation_footer_content').fadeIn(100).animate({height: '190px'}, 'fast', 'swing');
		return false;
	});
	$('#close').click(function(){
		$('.sdl_navigation_footer_content').animate({height: '18px'}, 'fast', 'swing', function() {
			$(this).fadeOut('fast');
		});
	});
	$('#nl_form').submit(function(){
		var formdata = $('#nl_form').serialize();
		$.get('sites/all/themes/sdl/newsletter-signup.php', formdata, function(data){
			$('#nl_form_output').remove();
			$('#sdl_newsletter_form h4').html(data);
			if (data == 'Thank you for joining Insight') {
				$('#nl_form').hide();
			}
		});
		return false;
	});
	/* MASONRY */
	var fade = true;
	var speed = 400;
	var cases = false;
	var selector = false;

	if ($('div.view-news').length > 0) {
		selector = 'div.view-news';
	} else if ($('div.view-cases').length > 0) {
		cases = true;
		selector = 'div.view-cases';
	}

	if (selector){
		$(selector + '> ul li').addClass('masonryItem');
		$(selector + '> ul').masonry({
			columnWidth: 100,
			itemSelector: '.masonryItem',
	        animate: true,
	        animationOptions: {
	            duration: 500,
	            queue: false
	        }
		});
		$(window).resize(function(){
			resizeMasonry(cases);
		});
	}
	

	function bugFix(){
		//BUG FIXING
		if ($("#cases_show_details").css('opacity') != 1){
			$(".case_images").css('height', '180px');
			$(".node-case").css('height', '255px');
			$(".node-case").css('padding', '0 0 50px 0');
		} else {
			$(".case_thumb_details").hide();
		}
	}

	$('#filtering_nav a').click(function(){
//		$(".case_images").css('height', '255px');
		var colorClass = '.' + $(this).attr('class');
		if (colorClass == '.all') {
			bugFix();
			$("#filtering_nav li a.active").removeClass('active');
			$(colorClass).addClass('active');

			// show all hidden boxes
			if (fade == true){
$('#primary .view-cases .masoned .masonryItem').children('.invis').not('.first').toggleClass('invis').animate({opacity: 1},{ duration: speed }).show();
			} else {
$('#primary .view-cases .masoned .masonryItem').children('.invis').toggleClass('invis').not('.first').css('opacity', '1').show();
			}
		} else if (colorClass=='.view1') {
			if ($("#cases_show_details").css('opacity') == 1){
				$(".case_images").animate({
					height: '180px'
				});
				$(".view1").fadeTo('slow', 0.35);
				$(".view2").fadeTo('slow', 1);
				$(".case_thumb_details").delay(500).fadeIn('slow');
			}
		} else if(colorClass=='.view2') {
			$(".case_thumb_details").fadeOut('slow');
			$(".view2").fadeTo('slow', 0.35);
			$(".view1").fadeTo('slow', 1);
			$(".case_images").delay(500).animate({
				height: '255px'
			});
		} else if (colorClass.indexOf('active') == -1){
			bugFix();
			// hide visible boxes 
			$("#filtering_nav li a.active").removeClass('active');
			$(colorClass).addClass('active');					
			if (fade == true){
$('#primary .view-cases .masoned .masonryItem').children().not(colorClass).not('.invis').not('.first').toggleClass('invis').animate({opacity: 0},{ duration: speed }).hide();
			} else {
$('#primary .view-cases .masoned .masonryItem').children().not(colorClass).not('.invis').not('.first').toggleClass('invis').css('opacity', '0');
			}

			// show hidden boxes
			if (fade == true){
$('#primary .view-cases .masoned .masonryItem').children(colorClass+'.invis').not('.first').toggleClass('invis').animate({opacity: 1},{ duration: speed }).show();
			} else {
$('#primary .view-cases .masoned .masonryItem').children(colorClass+'.invis').not('.first').toggleClass('invis').css('opacity','1');
			}
		}

		if (colorClass != ".view2" && colorClass != ".view1"){
			$(selector + '> ul li').addClass('masonryItem');
			$(selector + '> ul').masonry({
				columnWidth: 100,
				itemSelector: '.masonryItem',
				animate: true,
				animationOptions: {
					duration: 500,
					queue: false
				}
			});

			$(window).resize(function(){
				resizeMasonry(cases);
			});
			return false;
		}
	});
});

function frontSlideShow() {
	var active = $('img.active_image');
	var first = $('img.wallpaper:first');
	var next = active.next().length ? active.next() : first;
	active.removeClass('active_image');
	next.addClass('active_image').fadeIn('3500' ,function(){
		active.css({'display':'none'});
	});
}
function resizeCase(){
	var winWidth = $(window).width();
	if (winWidth > 700) {
		var tmpLeft = ((winWidth-1000)/2)*-1;
		$("#case_elements").css('width',winWidth);
		$("#case_elements").css('left',tmpLeft+'px');
		$("#case_elements").show();
		var tmpMargin = (winWidth-5000)/2;

		// Add 1px to margin when tmpMargin is a decimal
		if(tmpMargin < Math.round(tmpMargin) && $.browser.mozilla != true) tmpMargin -= 1;
		$("#case_elements .case_element").css('margin-left',(tmpMargin)+'px');
	}
	
	
}
function resizeMasonry(cases) {
	var masonWrap = $('.masoned');
	var tmpWidth = (cases ? 1000 : 800);
	var windowWidth = $(window).width();
	
	if (windowWidth > 1060){ 
		masonWrap.css('width', tmpWidth);
	} else if(windowWidth < 1060){
		if (windowWidth < 630) {tmpWidth = 200;}
		else if (windowWidth < 830) {tmpWidth = 400;}
		else if (windowWidth < 1030) {tmpWidth = 600;}
		if (cases && tmpWidth < 1000) {tmpWidth += 200;}
		masonWrap.css('width', tmpWidth+'px');
	}


}

	$('.block-mailchimp h3').addClass('closed');
	$('.block-mailchimp h3').append('<img class="arrow" src="/en/sites/all/themes/sdl/images/newsletter-arrow-normal.gif">');
			$("#mailchimp_subscribe_anon_form_5d6d001158").prepend("<h4>Join our<br> newsletter and get the Insight</h4>");

$("#edit-mailchimp-lists-5d6d001158-EMAIL-wrapper input").attr("placeholder", "Email");
			$("#edit-mailchimp-lists-5d6d001158-NAME-wrapper input").attr("placeholder", "Name");
			$("#edit-mailchimp-lists-5d6d001158-COMPANY-wrapper input").attr("placeholder", "Company");
			$("#edit-mailchimp-lists-5d6d001158-EMAIL-wrapper label").replaceWith("<label>Email</label>");
			$("#edit-mailchimp-lists-5d6d001158-NAME-wrapper label").replaceWith("<label>Name</label>");
			$("#edit-mailchimp-lists-5d6d001158-COMPANY-wrapper label").replaceWith("<label>Company</label>");




function newsletter() {
	if ($('.block-mailchimp h3').hasClass('closed')) {
		$('.block-mailchimp h3').toggleClass('closed');
		$('.block-mailchimp').animate({
			top: '68px',
			height: '182px'}, 300, function () {
				
			});
			
			$('.block-mailchimp h3').html('x');
				$("#mailchimp_subscribe_anon_form_5d6d001158").fadeIn(50);
				$("#mailchimp_subscribe_anon_form_5d6d001158 h4").fadeIn(50);
		
		
	} else {
		$('.block-mailchimp h3').toggleClass('closed');
		$('.block-mailchimp h3').html('Subscribe to newsletter');
		$('.block-mailchimp h3').append('<img class="arrow" src="/en/sites/all/themes/sdl/images/newsletter-arrow-normal.gif">');
		$('.block-mailchimp').animate({top: '239px',height:'10px'}, 200);
		$("#mailchimp_subscribe_anon_form_5d6d001158").slideUp(200);
		$("#mailchimp_subscribe_anon_form_5d6d001158 h4").slideUp(200);
	}


}

function activatePlaceholders() {
var detect = navigator.userAgent.toLowerCase(); 
if (detect.indexOf("safari") > 0) return false;
var inputs = document.getElementsByTagName("input");
for (var i=0;i<inputs.length;i++) {
  if (inputs[i].getAttribute("type") == "text") {
   if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
    inputs[i].value = inputs[i].getAttribute("placeholder");
    inputs[i].onclick = function() {
     if (this.value == this.getAttribute("placeholder")) {
      this.value = "";
     }
     return false;
    }
    inputs[i].onblur = function() {
     if (this.value.length < 1) {
      this.value = this.getAttribute("placeholder");
     }
    }
   }
  }
}
}
window.onload=function() {
activatePlaceholders();
}
