// Delay Plugin for jQuery
// - http://www.evanbot.com
// - copyright 2008 Evan Byrne
/*
 * Jonathan Howard
 * jQuery Pause
 * version 0.2
 * Requires: jQuery 1.0 (tested with svn as of 7/20/2006)
 * Feel free to do whatever you'd like with this, just please give credit where
 * credit is do.
 * pause() will hold everything in the queue for a given number of milliseconds,
 * or 1000 milliseconds if none is given.
 */
// Wait Plugin for jQuery
// http://www.inet411.com
// based on the Delay and Pause Plugin
 (function($) {
    $.fn.wait = function(option, options) {
        milli = 1000; 
        if (option && (typeof option == 'function' || isNaN(option)) ) { 
            options = option;
        } else if (option) { 
            milli = option;
        }
        // set defaults
        var defaults = {
            msec: milli,
            onEnd: options
        },
        settings = $.extend({},defaults, options);

        if(typeof settings.onEnd == 'function') {
            this.each(function() {
                setTimeout(settings.onEnd, settings.msec);
            });
            return this;
        } else {
            return this.queue('fx',
            function() {
                var self = this;
                setTimeout(function() { $.dequeue(self); },settings.msec);
            });
        }

    }
})(jQuery);

//General

jQuery.noConflict();

jQuery(function($) {
	
	//Portfolio Nav
	$('#n-2 ul').click(function(event){
		if ($(event.target).is('a')) {			
			$(this).slideUp('fast', function(){
				$(this).css('left','-9999px');
			});
			
			//Animation Icon down
			$el = $(this).siblings('span');
			navAnimateDown($el, 11);
			
		}
	}).find('a').each(function(){
		id = 'item-'+this.id.substr(2);
		name = $('.item[rel='+id+']').attr('id');
		this.href += '#'+name;
	});
	$('#n-2 > a').mouseenter(function(){
		$(this).siblings('ul').show();
	});
	
	
	//Scrolling
	$('#nav').localScroll();
	
	//Modifying URL based on selected menu item
	$('#n-2 ul a').bind('click', function(){
		url = this.href.split('#')[1];
		window.location.hash = url
	});
	
	//Animating the icon
	$('#n-2').mouseenter(function(){
		$el = $(this).find('>span');
		navAnimateUp($el, 0);
	}).mouseleave(function(){
		$el = $(this).find('>span');
		navAnimateDown($el, 11);
	});	

	
	//Portfolio Tabs
	$('ul.tabs').each(function(){
		$('a', this).each(function(i){
			i++;
			if (i==1) $(this).addClass('active');
			$(this).wrapInner('<span></span>').prepend('<strong>'+i+'</strong> ');
		});		
	});	

	$('div.tabs').each(function(){
		var $item = $(this);
		var $parent = $item.parent();
		var $images = $parent.parents('.content-wrapper').siblings('.image').find('.i');
		var $imageWrap = $parent.parents('.content-wrapper').siblings('.image');
		$item.tabs({
			fx: {height:'toggle'},
			show: function(event, ui) {
				if ($images.filter('.image-'+ui.index).length > 0) { 
					$imageWrap.css('height', $imageWrap.height());
					$images
						.find('img').css({ 'position': 'absolute' }).end()
						.filter('.image-'+ui.index).find('img').css({ 'position': 'static' }).fadeIn('normal', function(){						
							$images.find('img').not(this).hide().css({ 'position': 'static' });
							$imageWrap.css('height','auto');
						});
				}
				return false;
			}
		});		
	});
	
	//Show / Hide Portfolio
	$('.item .action').click(function(){
		var $el = $(this);
		var $panels = $el.siblings('.tabs').find('.panels');
		if ($el.is('.show')){
			$panels.slideDown('normal');
			$el.removeClass('show').addClass('hide');
		} else if ($el.is('.hide')){
			$panels.slideUp('normal');
			$el.removeClass('hide').addClass('show');
		}
	});
	
	//Read More
	$('.readMore').siblings('.content').find('.text').css({
		'height':'55px',
		'margin-bottom':'5px'
	});
	
	//Byraet
	//TODO rescrie. innerShadow
	$('.box').each(function(){
		
		var $box = $(this); //Box
		var $action = $('.action', $box); //Action Link
		if ($action.is('.show')) { $box.addClass('closed'); }
		else { $box.removeClass('closed'); }
		
		$('.action', $box).click(function(){
			var $el = $(this);
			var $content = $el.siblings('.content'); //Content
			var $siblingButtons = $el.parent('.box').siblings('.box').find('.action'); 
			
			//Toggling the content
			if ($el.is('.show')){
				$content.slideDown('fast'); //Showing the content
				$el.removeClass('show').addClass('hide');
				$box.removeClass('closed');
				
				//Hiding other boxes, accordion style
				$siblingButtons.each(function(){
					if ($(this).is('.hide')) {
						$(this).click();
					}
				}); 
				
			} else if ($el.is('.hide')){
				$content.slideUp('fast', function(){
					$el.removeClass('hide').addClass('show'); //Hiding the content
					$box.addClass('closed');
				});				
			} else if ($el.is('.readMore')){
				$el.siblings('.content').find('.text').css({
					'height':'auto',
					'margin':'0'
				});			
				$el.removeClass('readMore').addClass('hide').text('LUKK');
			}
		});
		
	});	

	//Slideshow
	$('.slideshow').find('.image').each(function(){
		$(this).cycle();
		
		$('img:first', $(this)).clone().appendTo($(this)).css({
			'position': 'static',
			'visibility': 'hidden'
		});
	});

	//Temporary Box
	$('#temporary').width($('#nav').width()-34);
	
	$('#temporary iframe').parents('#temporary').addClass('youtube');
	
	$(window).scroll(function(){
		$('#temporary').fadeOut('slow');
	});
	
	//QTip
	$('#contact #content .download').qtip({
		position: {
			corner: {
				target: 'rightTop',
				tooltip: 'bottomLeft'
			}
         },
         style: { 
        	 width: 130,
        	 background: '#000',
        	 color: 'white',
        	 padding: '0',
        	 textAlign: 'center',
        	 tip: 'bottomLeft',
        	 border: {
        	 	width: 1,
        	 	radius: 5,
        	 	color: '#000'
         	}
        }
	});
	
	
	//Newsletter subscription
	$('#html-emails').attr('checked', true);
	
});

function navAnimateUp($el, i){ 		
	$el.wait(30, function(){
		position = -(i*25);			
		$el.css('background-position','0px '+position+'px');
		i++;
		if (i<=11) navAnimateUp($el, i);							
	});	
}

function navAnimateDown($el, i){ 		
	$el.wait(30, function(){
		position = -(i*25);			
		$el.css('background-position','0px '+position+'px');
		i--;
		if (i>=0) navAnimateDown($el, i);							
	});	
}

/*
 * 10 year anniversary
 */
jQuery(function($){
  var $el = $('#byr-tusj');
  $('.tabs strong', $el).remove();
  $('.action', $el).click();
});
