// 
//  kims.js
//  htmlcss
//  
//  Created by Tommy Stomlien on 2010-11-01.
//  Copyright 2010 Jimmy Royal. All rights reserved.
// 



// ===========
// = Plugins =
// ===========


// =========================
// = Products active state =
// =========================
;(function($) {
// Set product menu to active
$.fn.active_menu = function(options) {
  var opts = $.extend({}, $.fn.active_menu.defaults, options);

  return this.each(function() {
    var $this = $(this);

    // Support for the Metadata Plugin.
    var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
  	
	var hash = window.location.hash || false;
	var links = $(this).find('a');
	
	if( hash != false ) {
		var active = $(this).find('a[href="' + window.location.href + '"]');
		active.parent('li').addClass( opts.activeclass );
		$('#menu-produkter').children('li').removeClass( opts.activeclass );
	}
	
	links.bind('click', function(ev) {
		links.parent('li').each( function() { $(this).removeClass( opts.activeclass ) } );
		$(this).parent('li').addClass( opts.activeclass );
		
		$('h2.sifr').closest('li').removeClass( opts.activeclass )
  	});
	
  });

  // private function for debugging
  function debug($obj) {
    if (window.console && window.console.log) {
      window.console.log($obj);
    }
  }
};

// default options
$.fn.active_menu.defaults = {
	activeclass: 'current-menu-item'
};

})(jQuery);



// =================
// = Equal Heights =
// =================

;(function($) {
//  Created by Tommy Stomlien on 2010-10-21.
// Set equal heights to HTML-elements
$.fn.equalheights = function( options ) {
  var opts = $.extend( {}, $.fn.equalheights.defaults, options );
  var h = 0;
  var i = 0;
  var elements = $(this);

  return this.each( function() {
    var $this = $(this);
    // Support for the Metadata Plugin.
    var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

	if( $this.height() > h ) {
		h = $this.height();
	}
	
	i++;
	
	if( elements.length == i ) {
	    $(elements).css( 'height', h + "px" );
	}
		
  });
};

// default options
$.fn.equalheights.defaults = {
	// no defaults
};

})(jQuery);


// =============
// = Slideshow =
// =============
;(function($) {
// slideshow
$.fn.slideshow = function(options) {
  var opts = $.extend({}, $.fn.slideshow.defaults, options);
  var active_item;
  var slide_interval = 0;
  var adjusted = false;
  var container;
  var items;
 
  return this.each(function() {
	var $this = $(this);
	
    // Support for the Metadata Plugin.
    var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
  	
	container = $(opts.container);
	container.hide();
	items = container.find('li');
	
	// wait for image load
	$(window).load(function() {
		container.fadeIn();
		if(!adjusted) adjust_margins( $this );
	
		if( !active_item ) {
			active_item = $this.find('li:first');
			last_item = $this.find('li:last');
			last_item.addClass('last');
			last_item.after(active_item.clone());
		} 
		
		setTimeout(function() {
			if( items.length > 1 ) {
			slide_interval = setInterval(function() {			
				var dx = active_item.outerWidth();
				var ml = Math.abs( parseInt( $this.css('marginLeft').replace('px', '') ) );
				var w = $('#product_slideshow_container').width();
			

				$this.animate({ marginLeft: -(w + ml) }, 200, function() {
					if( active_item.hasClass('last') ) {
						active_item = $this.find('li:first');
						$this.css('marginLeft', '0px');
					} else {
						active_item = active_item.next('li');
					}
				
				});
			
				}, opts.interval);
			}
		}, opts.wait_for);
	});

  });

  function adjust_margins( el ) {
    var h = $('#product_slideshow_container').height();
	
	el.find('li > a > img').each(function() {
		$(this).css('marginTop', opts.container_height - $(this).height() - 32 + "px");
	});
	
	adjusted = true;
  }

  // private function for debugging
  function debug($obj) {
    if (window.console && window.console.log) {
      window.console.log($obj);
    }
  }
};

// default options
$.fn.slideshow.defaults = {
	interval: 6000,
	container: '#product_slideshow_container',
	container_height: 310,
	wait_for: 0
};

})(jQuery);




// ======================
// = Facebook Slidedown =
// ======================
;(function($) {
// Slides down on top of page
$.fn.slider = function(options) {
  var opts = $.extend({}, $.fn.slider.defaults, options);

  return this.each(function() {
    var $this = $(this);

    // Support for the Metadata Plugin.
    var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
	
	// fix margin
	$content = $this.find( opts.slideContent );
	$c_h = $content.height();
	$content.css( 'display','none' );
	
	// button
	$button = $( opts.button );
	
	$button.bind('click', function(ev) {
		$content.slideToggle();
		$button.toggleClass( 'fb_active' );
		ev.preventDefault();
	});
	

  });

  // private function for debugging
  function debug($obj) {
    if (window.console && window.console.log) {
      window.console.log($obj);
    }
  }
};

// default options
$.fn.slider.defaults = {
  slideContent: '#slidedown_content',
  button: '#fb_follow'
};

})(jQuery);



// =============
// = DOM-ready =
// =============
$(function() {
	$('#fb_slidedown').slider();
	
	setTimeout( function() {
	   $('#fb_slidedown').css( 'display', 'block' );
	}, 1000 );
	
	
    $('#kosestrom').fadeTo(0, 0);
    
    
    $('#kosestrom').find('object').each(function() {
    	$(this).attr( { width: '284px', height: '236px' });
    	$(this).find('embed').attr( { width: '284px', height: '236px' });
    });
	
	var p_slideshow = $('#product_slideshow');
	var s_slideshow = $('#classics_slideshow');
	var menu_product = $('#menu-produkter');
	
	// product slideshow
	if( p_slideshow.length ) {
		p_slideshow.slideshow();		
	}
	
	// classics slideshow
	if( s_slideshow.length ) {
		s_slideshow.slideshow( { wait_for: 3000, container: '#classics_slideshow_container' });		
	}
	
	// menu product active state
	if( menu_product.length ) {
		menu_product.active_menu();
	}
	
    var kos = $('#kosestrom');    
    if( kos.length ) {
    	// hide title if kosestrøm-post contains video
        var youtube = $('.youtube');
        youtube.closest('.kos').find('h5').hide();
        
        // remove wordpress youtube plugin paragraph-margin
        youtube.closest('p').css({ margin: '0' });
        
        // add masonry to frontpage
        setTimeout(function() {
            $('#kosestrom').masonry({ singleMode: true, animate: true, columnWidth: 300, itemSelector: '.kos' }, function() { $('#kosestrom').fadeTo(1, 500) });
        }, 300);
        
    }
    
    /* adjust margin on product_listing */
    $('.image_wrapper').find('img').each( function() {
        var ph = 160;
        var h = $(this).height();
        
        $(this).css({ marginTop: ( ph - h - 10) + "px" });
            
    } );
    

    var flashvars = false;
    var params = { wmode: 'transparent' };
    var attributes = { };

    swfobject.embedSWF("../wp-content/themes/kims/flash/Countdown.swf", "countdown_flash", "294", "96", "10.0.0", false, flashvars, params, attributes);    
});


// ============
// = Tracking =
// ============

function ga_track() {
    _gaq.push(['_trackPageview', path]);
}
