/*
CSS Browser Selector v0.3.3 (Sep 09, 2009)
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
function css_browser_selector(u){var ua = u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1;},g='gecko',w='webkit',s='safari',h=document.getElementsByTagName('html')[0],b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3')?g+' ff3':is('gecko/')?g:/opera(\s|\/)(\d+)/.test(ua)?'opera opera'+RegExp.$2:is('konqueror')?'konqueror':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?'mobile':is('iphone')?'iphone':is('ipod')?'ipod':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win':is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);

/*
 * doTimeout - v0.4 - 7/15/2009
 * http://benalman.com/projects/jquery-dotimeout-plugin/
 * 
 * Copyright (c) 2009 "Cowboy" Ben Alman
 * Licensed under the MIT license
 * http://benalman.com/about/license/
 */
(function($){var a={},c="doTimeout",d=Array.prototype.slice;$[c]=function(){return b.apply(window,[0].concat(d.call(arguments)))};$.fn[c]=function(){var e=d.call(arguments),f=b.apply(this,[c+e[0]].concat(e));return typeof e[0]==="number"||typeof e[1]==="number"?this:f};function b(l){var m=this,h,k={},n=arguments,i=4,g=n[1],j=n[2],o=n[3];if(typeof g!=="string"){i--;g=l=0;j=n[1];o=n[2]}if(l){h=m.eq(0);h.data(l,k=h.data(l)||{})}else{if(g){k=a[g]||(a[g]={})}}k.id&&clearTimeout(k.id);delete k.id;function f(){if(l){h.removeData(l)}else{if(g){delete a[g]}}}function e(){k.id=setTimeout(function(){k.fn()},j)}if(o){k.fn=function(p){o.apply(m,d.call(n,i))&&!p?e():f()};e()}else{if(k.fn){j===undefined?f():k.fn(j===false);return true}else{f()}}}})(jQuery);

$(document).ready(function() {

    $("div.box-title a").each(function() {
	    $(this).wrapInner("<span></span>");
    });
    
    // remove tooltips
    $(".col-right .thumb-box a, ul.illustrators a").hover(
      function () {
      	$(this).attr("title", "");
      }
  	);
    // side navigation mouseovers
    $("div#col-right .thumb-box img").attr("alt", "")
    $("div#col-right .thumb-box").hover(
      function () {
      	//$(this).contents().find("a").attr("title", "");
      	$(this).contents().find("img").hide();
      }, 
      function () {
        $(this).contents().find("img").fadeIn("fast");
      }
  	);
  	
  	// easing for animation
	$.easing.custom = function (x, t, b, c, d) 
	{ 
	    // easeInOutQuint from http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	}
	
	// anti-spam email
	$('.mail').each(function() 
	{
	    var obfAddress = $(this).text();
	    var realAddress = obfAddress.replace(/\[at\]/g, '@').replace(/\[dot\]/g, '.');
	    var emailLink = document.createElement('a');
	    $(emailLink).attr(
	    {
	        'href': 'mailto:' + realAddress,
	        'title': 'Email: ' + realAddress,
	        'class': 'email-address'
	    }).text(realAddress);
	    $(this).replaceWith(emailLink);
	});
    
    // input clearing on focus
    $.fn.clearInputText = function() {
		return this.focus(function() {
			if( this.value == this.defaultValue) {
				this.value = "";
			}
		}).blur(function() {
			if(!this.value.length) {
				this.value = this.defaultValue;
			}
		});
	};
    
    // jQuery SmoothScroll
	// from http://blog.medianotions.de/en/articles/2009/smoothscroll-for-jquery
	$('a.target[href*=#], a.toggler[href*=#]').click(function() {
	    
		// make sure it's the same location      
	   if(location.pathname.replace(/^\//,'')==this.pathname.replace(/^\//,'') && location.hostname==this.hostname && this.hash.replace(/#/,'')) {
	      // get parameters
	      var hash=this.hash;
	      var target=$(hash).offset().top;
	      
	      // animate to target and set the hash to the window.location after the animation
	      $.doTimeout(300, function(){ // wait a second
	    	//console.log("hammertime!");
	    	$("html:not(:animated),body:not(:animated)").animate({ scrollTop: target }, 1500, "custom", function() { /*location.hash=hash;*/ });
		  });
	      // cancel default click action
	      return false;
	   }
	});
	
	// link back
	$('a.back').click(function() {
		history.back();
		return false;
	});
    
    // toggler
	$("a.toggler").addClass('toggler-off');
	$('#view_work').css('overflow', 'auto');
	$('#view_work').css('height', 'auto');
	$('#view_work ul').hide();

	$.fn.toggler = function(div) {	
		
		$(div).slideToggle(175);
    	if($(this).is('.toggler-on') ) {
          $(this).removeClass('toggler-on').addClass('toggler-off');
        } else {
          $(this).removeClass('toggler-off').addClass('toggler-on');
        }
        return false;
	};

	// initialize scrollables
    $("div.scrollable").scrollable(
    {
    	size: 1,
    	loop: true,
    	easing: "custom",
    	speed: 500,
    	next: ".scrollable-next",
    	prev: ".scrollable-prev",
    	disabledClass: "disabled",
    	keyboard: "static",
    	onSeek: function(event, i) { 
        	//console.log("current page is " + this.getPageIndex()); 
    	}
    }).navigator(
    {
    	navi: ".scrollable-navi"
    });
        
    $("div.thumb-wrapper").scrollable(
    {
    	size: 1,
    	clickable: false,
    	loop: false,
    	vertical: true,
    	easing: "custom",
    	speed: 500,
    	disabledClass: "disabled",
    	onSeek: function(event, i) { 
        	//console.log("current page is " + this.getPageIndex()); 
    	}
    }).navigator(
    {
    	navi:'ul.thumb-navi' 
    }).navigator(
    {
    	navi:'ul.thumb-navi2' 
    });
    
    function video_player() {
	    
	    flowplayer("video-player", "http://www.pekkafinland.fi/js/flowplayer/flowplayer.commercial-3.1.5.swf", { 
	    key: '#$f68634de9d01596d5c5',
	    
	    onLoad: function() {   
	        this.setVolume(40);  
	    },
	    clip: { 
	        autoPlay: false, 
	        autoBuffering: true, 
	        scaling: 'fit'
	    },
	    canvas: {
	    	backgroundColor: '#000000',
	    	backgroundGradient: 'none'
	    },
	    plugins: {
	      controls: {
	    	timeColor: '#ffffff',
	      	volumeSliderColor: '#333333',
	      	tooltipColor: '#ffffff',
	      	backgroundGradient: 'none',
	      	volumeSliderGradient: 'none',
	      	buttonOverColor: '#4C82CE',
	      	tooltipTextColor: '#454545',
	      	sliderGradient: 'none',
	      	sliderColor: '#000000',
	      	borderRadius: '0',
	      	buttonColor: '#333333',
	      	backgroundColor: '#111111',
	      	progressColor: '#4C82CE',
	      	bufferColor: '#333333',
	      	timeBgColor: '#111111',
	      	bufferGradient: 'none',
	      	durationColor: '#ffffff',
	      	progressGradient: 'none',
	      	height: 32,
	      	opacity: 1.0
	      }
		}
		});
	}
	if($('a#video-player').length) video_player();
	
    
    //
    // ajax loading
	//
	
	var ajax_load = "<img src='js/ajax-loader.gif' alt='Loading...' style='margin: 30px 0 0 294px' />";  
	
    $("body#work div#col-right .thumb-box a.link").click(function(){  
		
		var ajaxUrl = $(this).attr('href');
        var ajaxId = ajaxUrl.split("=");
           
        $("#ajax-container").
        html(ajax_load).
        load($(this).attr('href') + " #ajax-container", function(){  
			
			if($('a#video-player').length) video_player();
			
        	//$(".addthis_button").attr("addthis:url", $.new_addthis_prefix + '/work/?id=' + ajaxId[1]);
        	$(".scrollable").addClass("scrollable-" + ajaxId[1]);
            $(".scrollable-next").addClass("scrollable-next-" + ajaxId[1]);
            $(".scrollable-prev").addClass("scrollable-prev-" + ajaxId[1]);
            $(".scrollable-navi").addClass("scrollable-navi-" + ajaxId[1]);
            
            $("div.scrollable-" + ajaxId[1]).scrollable(
    		{
		    	size: 1,
		    	loop: true,
		    	easing: "custom",
		    	speed: 500,
		    	next: ".scrollable-next-" + ajaxId[1],
		    	prev: ".scrollable-prev-" + ajaxId[1],
		    	disabledClass: "disabled",
		    	keyboard: "static"
		    }).navigator(
		    {
		    	navi: ".scrollable-navi-" + ajaxId[1]
		    });
        });
        
    	return false;
    });
    
    $("form#mailinglist").submit(function() {
		if ($("form#mailinglist #Name").val() == "" || $("form#mailinglist #Email").val() == "") {
		   	$('form#mailinglist p.error').html("Please, fill in all fields.").fadeIn();
		   	return false;
		} 
	});
});