/*
Copyright (c) 2007 Mediasparx (http://www.mediasparx.com)
Notes: Forsiden visning
*/
$.preloadImage = new Image();
$.pluginObjArray = new Array();
$.fn.prosjekter = function(passedOptions) {
	var options = {
        current: 0,
        currentImage: 0,
		source: '',
        imageSource: '',
        image: '',
		speed: 'slow',
        delay: 6000,
        projectAutoPlay: 'off',
		ArrayIndex: -1,
		dataXML: {},
		timerID: -1,
		setXML: function(ArrayIndex) {
			$.get(options.source, function(data){
				options.dataXML = data;
				options.setStage();
			});
		},
		setStage: function(ArrayIndex) {
            $('#media-left').empty().append('<div id="imageLoading"><img src="/momenta/frontend/files/gfx/loading.gif"></div><div id="imageShowing" style="padding-left: 7px;"></div>').height(329);
            $("#imageShowing").hover(function(){
                clearTimeout(options.timerID);
                if (options.projectAutoPlay != 'off') {
                    $("#imageShowing").css('cursor', 'progress');
                }
            },function(){
                if (options.projectAutoPlay != 'off') {
                    options.timerID = setTimeout('$.pluginObjArray['+ options.ArrayIndex +'].setContent();', (options.delay/2));
                    $("#imageShowing").css('cursor', 'default');
                }
            });
            options.setMenu();
		},
	    setMenu: function() {
            $('#hover li a').removeAttr('href').css('cursor', 'pointer');
            $('#hover li a').click(function(){
                clearTimeout(options.timerID);
                $('#imageShowing').hide();
                $('#imageLoading').show();
                $('#hover li').removeClass('selected').removeAttr('hint');
                $(this).parent('li').addClass('selected').attr('hint','selected');
                var counter = 0;
                while ( $('#hover li:eq('+counter+')').attr('hint') != 'selected' ) {
                    counter++;
                }
                options.current = counter;
                options.currentImage = 0;
                options.projectAutoPlay = 'on';
                options.setContent();
                return false;
            });
            options.setContent();
		},
		setImage: function() {
            $.preloadImage.onload = function() {
                $('#imageLoading').hide();
                $('#imageShowing').empty().append('<img src="' + options.imageSource + options.image + '" />' ).fadeIn(options.speed);
                if (options.projectAutoPlay != 'off') {
                    $('.bildebarPause').show(); // lagt til 24.5
                    $('.bildebarPlay').hide(); // lagt til 24.5
                    options.timerID = setTimeout('$.pluginObjArray['+ options.ArrayIndex +'].setContent();', options.delay);
                } else { // lagt til 24.5
                    $('.bildebarPlay').show(); // lagt til 24.5
                    $('.bildebarPause').hide(); // lagt til 24.5
                } // lagt til 24.5
            };
            $.preloadImage.src = options.imageSource + options.image;
        },
		setContent: function() {
            if (options.projectAutoPlay != 'off') {
                $('#imageShowing').hide();
                $('#imageLoading').show();
                if(content_id > 0) {
//alert("COUNTER:"+options.current+" ,CONTENT_ID="+content_id+" ,LENGTH="+$('item', options.dataXML).length);
                  var element = 0;
                  while(element < $('item', options.dataXML).length) {
                    var localItem = $('item', options.dataXML).get(element);
                    var localContent_id = $('content_id', localItem).text();
//alert("ELEMENT="+element+" ,localCOnTENT_ID="+localContent_id);
                    if(content_id == localContent_id) {
                      options.current = element;
                    }
                    element++;
                  }
                  content_id = 0;
                }
                var Item = $('item', options.dataXML).get(options.current);
                var Description = $('description', Item).text();
                var Details = $('details', Item).text();
                var imageArray = new Array();
                counter = 0;
                $('images', Item).find('image').each(function(){
                    imageArray[counter] = $(this).text();
                    counter++;
                });
                if (options.currentImage == imageArray.length) {
                    options.currentImage = 0;
                    var nullstill = 'On'; // lagt til 24.5
                } else if (options.currentImage == 0) {
                    $('#hover li').removeClass('selected');
                    $('#hover li:eq('+ options.current +')').addClass('selected');
                    $('#innhold-left').empty().append(Description).fadeIn('slow');
                    $('#innhold-right').empty().append(Details).fadeIn('slow');
                    TB_init();
                }
                options.image = imageArray[options.currentImage];
                options.setImage();
                if (options.currentImage == 0 && imageArray.length > 1 && !nullstill) {
                    options.projectAutoPlay = 'on';
                    var prosjektbilder = '<p><a class="bildebarPlay"><img src="/momenta/frontend/elements/icons/play.gif" border="0" alt="Play"></a><a class="bildebarPause"><img src="/momenta/frontend/elements/icons/pause.gif" border="0" alt="Play"></a> &nbsp; <a class="selected">1</a>';
                    counter = 1;
                    while (counter < imageArray.length) {
                        prosjektbilder += ' &nbsp; <a>' + (counter+1) + '</a>';
                        counter++;
                    }
                    prosjektbilder += ' &nbsp; </p>';
                    $('#bildebar-left').empty().append(prosjektbilder).fadeIn('slow');
                    $('.bildebarPlay').hide(); // lagt til 24.5
                    $('#bildebar-left a').css('cursor', 'pointer');
                    $('#bildebar-left a').click(function(){
                        if($(this).attr("class") == 'bildebarPlay') {
							clearTimeout(options.timerID);
                                                        if(options.currentImage == imageArray.length) {
                                                                options.currentImage = 0;
                                                        }
							options.projectAutoPlay = 'on';
		                    options.timerID = setTimeout('$.pluginObjArray['+ options.ArrayIndex +'].setContent();', options.delay);
		                    $('.bildebarPause').show(); // lagt til 24.5
		                    $('.bildebarPlay').hide(); // lagt til 24.5
	                        return false;
                        } else if($(this).attr("class") == 'bildebarPause') {
							clearTimeout(options.timerID);
							options.projectAutoPlay = 'off';
		                    $('.bildebarPlay').show(); // lagt til 24.5
		                    $('.bildebarPause').hide(); // lagt til 24.5
	                        return false;
                        } else {
                          $('#bildebar-left a').removeClass('selected').removeAttr('hint');
                          $(this).addClass('selected').attr('hint','selected');
                          options.projectAutoPlay = 'off';
                          var counter = 0;
                          while ( $('#bildebar-left a:eq('+counter+')').attr('hint') != 'selected' ) {
                              counter++;
                          }
                          options.currentImage = counter - 2;
                          options.image = imageArray[options.currentImage];
                          options.setImage();
                          options.currentImage++;
                          return false;
                        }
                    });
                } else if (options.currentImage >= 0 && imageArray.length > 1) { // endret 24.5
                    $('#bildebar-left a').removeClass('selected');
                    $('#bildebar-left a:eq('+ (options.currentImage+2) +')').addClass('selected'); // endret 29.5
                } else {
                    options.projectAutoPlay = 'off';
                    $('#bildebar-left').empty();
                }
                options.currentImage++;
            }
        }
	};
	if (passedOptions) {
		$.extend(options, passedOptions);
	}
	return this.each(function(){
		options.pluginID = this.id;
		$.pluginObjArray.push(options);
		options.ArrayIndex = $.pluginObjArray.length - 1;
		$.pluginObjArray[options.ArrayIndex].setXML(options.ArrayIndex);
	});
};
$(function() {
    $('#wrap').prosjekter({
	    source: '/momenta/frontend/files/xml/forsiden.xml',
        imageSource: '/momenta/frontend/files/CONTENT/',
    	speed: 'slow',
   		delay: 6000,
        projectAutoPlay: 'on'
   	});
});
