// JavaScript Document

/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/

$.fn.equals = function(compareTo) {
        if (!compareTo || !compareTo.length || this.length!=compareTo.length) 
{ 
                return false; 
        } 
        for (var i=0; i<this.length; i++) { 
                if (this[i]!==compareTo[i]) { 
                        return false; 
                } 
        } 
        return true; 



} 

$.fn.overlabel = function( options ) {
 
        // build main options before element iteration
        var opts = $.extend( {}, $.fn.overlabel.defaults, options );
 
        var selection = this.filter( 'label[for]' ).map( function() {
 
            var label = $( this );
            var id = label.attr( 'for' );
            var field = document.getElementById( id );
 
            if ( !field ) return;
 
            // build element specific options
            var o = $.meta ? $.extend( {}, opts, label.data() ) : opts;
 
            label.addClass( o.label_class );
 
            var hide_label = function() { label.css( o.hide_css ) };
            var show_label = function() { this.value || label.css( o.show_css ) };
 
            $( field )
                 .parent().addClass( o.wrapper_class ).end()
                 .focus( hide_label ).blur( show_label ).each( hide_label ).each( show_label );
 
            return this;
 
        } );
 
        return opts.filter ? selection : selection.end();
    };
 
    // publicly accessible defaults
    $.fn.overlabel.defaults = {
 
        label_class:   'overlabel-apply',
        wrapper_class: 'overlabel-wrapper',
        hide_css:      { 'text-indent': '-10000px' },
        show_css:      { 'text-indent': '0px', 'cursor': 'text' },
        filter:        false
 
    };


function slideSwitch() {
    var $active = $('#slideshow DIV.active');

    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );

    if ( !$active.equals($next)) {
    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
        }
}

$(document).ready(function() {
   setInterval( "slideSwitch()", 15000 );
   var randomNumCase = Math.floor(Math.random()*($('div#randomCase div').length));
   $('div#randomCase div:eq(' + randomNumCase + ')').css("display", "block");
   var randomNumProduct = Math.floor(Math.random()*($('div#randomProduct div').length));
   $('div#randomProduct div:eq(' + randomNumProduct + ')').css("display", "block");
   $("label.overlabel").overlabel();
   $("#tabs").tabs();
   $("#browser").treeview();
   $('#menubar').corner("round 5px");
   $('#id_of_gallery').gallery({  interval: 5500,  height: '700px',  width: '700px', showOverlay: false});
//   var heighestCol = Math.max(Math.max($('#fakta_boks').height(),$('#maincontent').height()),$('#leftsidebar').height());
   var heighestCol = Math.max($('#maincontent').height(),$('#leftsidebar').height());
   if (!$('#tabs').length ) {$('#maincontent').height(heighestCol);} // if tabs format dont mess with height
   $('#fakta_boks').height(heighestCol-83); // ISO logo on top
   $('#leftsidebar').height(heighestCol);
   $('#slide').corner("round 10px");
		//thickbox replacement
	var closeModal = function(hash)
	{
		var $modalWindow = $(hash.w);
		//$('#jqmContent').attr('src', 'blank.html');
		$modalWindow.fadeOut('2000', function()
		{
			hash.o.remove();
			//refresh parent
			if (hash.refreshAfterClose == true)
			{
				window.location.href = document.location.href;
			}
		});
	};
	var openInFrame = function(hash)
	{
		var $trigger = $(hash.t);
		var $modalWindow = $(hash.w);
		var $modalContainer = $('iframe', $modalWindow);
		var myUrl = $trigger.attr('href');
		var myTitle = $trigger.attr('title');
		var newWidth = 0, newHeight = 0, newLeft = 0, newTop = 0;
		$modalContainer.html('').attr('src', myUrl);
		$('#jqmTitleText').text(myTitle);
		myUrl = (myUrl.lastIndexOf("#")> -1) ? myUrl.slice(0, myUrl.lastIndexOf("#")) : myUrl;
		var queryString = (myUrl.indexOf("?")> -1) ? myUrl.substr(myUrl.indexOf("?") + 1) : null;
		if (queryString != null && typeof queryString != 'undefined')
		{
			var queryVarsArray = queryString.split("&");
			for (var i = 0; i <queryVarsArray.length; i++)
			{
				if (unescape(queryVarsArray[i].split("=")[0]) == 'width')
				{
					var newWidth = queryVarsArray[i].split("=")[1];
				}
				if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'height')
				{
					var newHeight = queryVarsArray[i].split("=")[1];
				}
				if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'jqmRefresh')
				{
					hash.refreshAfterClose = queryVarsArray[i].split("=")[1]
				} else
				{
					hash.refreshAfterClose = true;
				}
			}
			// let's run through all possible values: 90%, nothing or a value in pixel
			if (newHeight != 0)
			{
				if (newHeight.indexOf('%')> -1)
				{
					newHeight = Math.floor(parseInt($(window).height()) * (parseInt(newHeight) / 100));
				}
				var newTop = Math.floor(parseInt($(window).height() - newHeight) / 2);
			}
			else
			{
				newHeight = $modalWindow.height();
			}
			if (newWidth != 0)
			{
				if (newWidth.indexOf('%')> -1)
				{
					newWidth = Math.floor(parseInt($(window).width() / 100) * parseInt(newWidth));
				}
				var newLeft = Math.floor(parseInt($(window).width() / 2) - parseInt(newWidth) / 2);
			}
			else
			{
				newWidth = $modalWindow.width();
			}
			// do the animation so that the windows stays on center of screen despite resizing
			$modalWindow.css({
				width: newWidth,
				height: newHeight,
				opacity: 0
			}).jqmShow().animate({
				width: newWidth,
				height: newHeight,
				top: newTop,
				left: newLeft,
				marginLeft: 0,
				opacity: 1
			}, 'fast');
		}
		else
		{
			// don't do animations
			$modalWindow.jqmShow();
		}
	}
	$('#modalWindow').jqm({
		overlay: 50,
		modal: true,
		trigger: 'a.thickbox',
		target: '#jqmContent',
		onHide: closeModal,
		toTop: true,
		onShow: openInFrame
	});

 

//		$('#maincontent').dropShadow();
   $("ul.sf-menu").superfish({pathClass:  'current',
                              speed:      'fast',    // faster animation speed
                              autoArrows: true      // if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation
        }); 

});

