var mycarousel_itemList = [];

if (window.innerWidth) {
	var w=window.innerWidth;
} else if (document.documentElement.clientWidth) {
	var w=document.documentElement.clientWidth; 
} 
// could also be screen.width

$(document).ready(function(){
	jQuery(".js_shownext").showHide({
		"accordion" : true,
		"findobj" : function (obj) { 
			return jQuery(obj).next("div.js_showhidearea"); 
		} 
	});
	jQuery(".openthis").slideDown(); 
	
	if (typeof(bghighsource) != "undefined") {
		var i=new Image();
		i.src=bghighsource;
		i.onload=function(){
			$("#scene").css("background-image","url("+i.src+")");
			$("#scene").fadeIn();
		}
	}
        if (jQuery('#slideshow_start').jcarousel) {
		if (w<=1024) {
			var wtype="_small";
		} else if (w>1024 && w<=1280) {
			var wtype="_med";
		} else if (w>1280) {
			var wtype="_big";
		}
		//alert(wtype)
		mycarousel_itemList = [
			{url: "img/start1"+wtype+".jpg", title: "Start1"},
			{url: "img/start2"+wtype+".jpg", title: "Start2"},
			{url: "img/start3"+wtype+".jpg", title: "Start3"},
			{url: "img/start4"+wtype+".jpg", title: "Start4"},
			{url: "img/start5"+wtype+".jpg", title: "Start5"},
			{url: "img/start6"+wtype+".jpg", title: "Start6"},
			{url: "img/start7"+wtype+".jpg", title: "Start7"},
			{url: "img/start8"+wtype+".jpg", title: "Start8"},
			{url: "img/start9"+wtype+".jpg", title: "Start9"}
		];
		// totally random:
		//shuffle = function(o){ //v1.0
		//	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
		//	return o;
		//}
		//
		//shuffle(mycarousel_itemList);
		// random startpoint:
		//	start : Math.round(Math.random()*mycarousel_itemList.length),
		//
		// random between certain numbers (1,4,7)
		var randarr=new Array(1,4,7);
		var startvalue=randarr[Math.floor(Math.random()*randarr.length)];
		carousel=jQuery('#slideshow_start').jcarousel({
			size: mycarousel_itemList.length,
			start : startvalue,
			scroll : 1,
			visible : 1,
			buttonNextHTML :  '<div class="jcarousel-next-horizontal"></div>',
			buttonPrevHTML : '<div class="jcarousel-prev-horizontal"></div>',
			itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback},
			initCallback :  init_carousel
		});
	}
        if (jQuery('#slideshow_ref').jcarousel) {
		carousel2=jQuery('#slideshow_ref').jcarousel({
			size: jQuery('ul#slideshow_ref li').size(),
			scroll : 1,
			visible : 1,
			buttonNextHTML : '<div class="jcarousel-next-horizontal"></div>',
			buttonPrevHTML : '<div class="jcarousel-prev-horizontal"></div>'
		});
	}
});
 $(window).bind("load", function() { 
	//jQuery("div#refpics").slideView();

});
function init_carousel(carousel) {  // actualy don't know why I need this, didn't work in this setup if we don't have this!
	carousel.setup();
}
function mycarousel_itemLoadCallback(carousel, state) {
    for (var i = carousel.first; i <= carousel.last; i++) {
        if (carousel.has(i)) {
            continue;
        }

        if (i > mycarousel_itemList.length) {
            break;
        }

        carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[i-1]));
    }
}

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
    return '<img src="' + item.url + '" alt="' + item.url + '" />';
};
		
/* 
	plugin: show/hide
	(c) 2008- Elin Tjerngren, Artopod
html example: 
	<div class="js_shownext"><a href="#"></a></div>
	<div class="js_showhidearea"></div>
	(  class="open" is added to "a" tag if not overridden )
	js:
	jQuery(".js_shownext").showHide();
	note: method findobj overrides property area
*/
(function(){
jQuery.fn.showHide = function(opt) {
	opt = jQuery.extend({
		accordion : false,
		area : ".js_showhidearea",
		openselector : "a",
		openclass : "open", 
		eventtype : "click",
		effect : "slide",
		defaultstate : { "display" : "none" }, // css values
		findobj : function(obj) { 
			return jQuery(obj).next(opt.area); 
		}
	}, opt);
	var $obj=this;
	return $obj.each(function() {
		var el=opt.findobj(this);
		var $h=el.get(0).offsetHeight;
		el.not("."+opt.openclass).css( opt.defaultstate ); // class="open" also overrides the default state...hm thats wierd maybe.
		jQuery(this).bind(opt.eventtype, function(e) {
			if (opt.effect=="slide") {
				el.slideToggle();
				if (opt.accordion) jQuery(opt.area).not(el).slideUp();
			} else if (opt.effect=="none") {
				el.toggle(); 
				if (opt.accordion) jQuery(opt.area).not(el).hide();
			}
			jQuery(this).children(opt.openselector).toggleClass(opt.openclass);
			return false;
		});
	});
};
})();

