var $j = jQuery.noConflict();

$j(function() {
    $j('a[rel*=external]').click( function() {
        window.open(this.href);
        return false;
    });
});

$j(document).ready(function() {

	$j('#flags img').click(function() {
		lang = $j(this).attr("lang");
		if($j("."+lang).is(":visible")){
			return false;
		}
		$j('#flags img').removeClass("active");
		$j(this).addClass("active");
		$j(".sv,.en").fadeOut();
		$j("."+lang+":hidden").fadeIn();
		
	});

	if ($j.browser.msie == false) {
		 initialize();	
	}	
	//$j("#commentform").validate();

	//$j("#slider").easySlider();
	$j.scrollTo.defaults.axis = 'y'; 
	$j.scrollTo( 0 );
	$j('.webbknapp').click(function(){
		$j().stop().scrollTo( '#webb', 1700, { easing:'easeOutExpo' } );
		return false;
	});
	$j('.fotoknapp').click(function(){
		$j().stop().scrollTo( '#foto', 1700, { easing:'easeOutExpo' } );
		return false;	
	});
	$j('.kontaktknapp').click(function(){
		$j().stop().scrollTo( '#kontakt', 1700, { easing:'easeOutExpo' } );
		return false;		
	});	
	$j('.startknapp').click(function(){
		$j().stop().scrollTo( '#start', 1700, { easing:'easeOutExpo' } );
		return false;		
	});	

	//move the image in pixel
	var move = -2;
	
	//zoom percentage, 1.2 =120%
	var zoom = 1.1;

	//On mouse over those thumbnail
	$j('.item').hover(function() {
		
		//Set the width and height according to the zoom percentage
		width = $j('.item').width() * zoom;
		height = $j('.item').height() * zoom;
		
		//Move and zoom the image
		$j(this).find('img').stop(false,true).animate({'width':width, 'height':height, 'top':move, 'left':move}, {duration:200});
		
		//Display the caption
		$j(this).find('div.text').stop(false,true).fadeIn(200);
	},
	function() {
		//Reset the image
		$j(this).find('img').stop(false,true).animate({'width':$j('.item').width(), 'height':$j('.item').height(), 'top':'0', 'left':'0'}, {duration:100});	

		//Hide the caption
		$j(this).find('div.text').stop(false,true).fadeOut(200);
	});


	$j('.gallery').addClass('gallery_demo'); // adds new class name to maintain degradability
	
	$j('ul.gallery').galleria({
		history   : false, // activates the history object for bookmarking, back-button etc.
		clickNext : true, // helper for making the image clickable
		insert    : '#main_image', // the containing selector for our main image
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
			
			// fade in the image and caption
			if(! ($j.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
				image.css('display','none').fadeIn(1000);
			}
			caption.css('display','none').fadeIn(1000);
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.3);
			
			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');
			
			// add a title for the clickable image
			image.attr('title','Nästa');
		},
		onThumb : function(thumb) { // thumbnail effects goes here
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// if thumbnail is active, fade all the way.
			var _fadeTo = _li.is('.active') ? '1' : '0.3';
			
			// fade in the thumbnail when finnished loading
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
			
			// hover effects
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
			)
		}
	});
	
	
	// Track menu links in analytics
	$j("a.button,#gallery_wrap ul li img").click(function(){
		pageTracker._trackPageview('/links/'+ $j(this).attr('rel'));
	});	
	
});

var mp;
function initialize() {
    mp = new mousepaint( {
        mode: "paint",               // "paint" or "erase"
        bg_image_url: "logo.jpg",    // if mode = "erase", bg image
        bg_image_opacity: "0.5",     // opacity of BG_IMAGE_URL
        square_width: 50,
        square_height: 50,
        square_color: "random",     // set to an RGB value or "random"
        toggle: false,  // default setting for toggle mode
        hud_enable: false,     // enable the heads up display options panel
        hud_text: "options"
	});
}

