/**
 * DOM data storage machanismus
 */
(function(){
 
    var cache = [0]; 
    var expando = 'data' + +new Date();
 
    function data(elem) {
 
        var cacheIndex = elem[expando];
        var nextCacheIndex = cache.length;
 
        if(!cacheIndex) {
            cacheIndex = elem[expando] = nextCacheIndex;
            cache[cacheIndex] = {};
        }
 
        return cache[cacheIndex]; 
    }
 
    window.data = data; 
})();

/**
 * Ein paar Metzhoden für Strings hinzufügen
 */
String.prototype.htmlEntities = function () {
   return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
};
String.prototype.htmlEntityDecode = function () {
   return this.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
};
String.prototype.trim = function () {
	return this.replace(/\s+$/,"").replace(/^\s+/,"");
};

Array.prototype.contains = function(obj) {
	var i, listed = false;
	for (i=0; i<this.length; i++) {
		if (this[i] === obj) {
			listed = true;
			break;
		}
	}
	return listed;
};

/**
 * Saalbach Namespace mit den speziellen Funktionen
 */
SAALBACH = {
	
	frontPage: {
		
		loadBlogRSS: function(containerId, feedUrl) {			
			jQuery.get(feedUrl, function(rssData){
				//console.log(rssData);
				var blogTeaserContent = '';	
				var entryCounter = 0;
				jQuery(rssData).find('item').each(function() {
					if (entryCounter < 4) {
						var entry = jQuery(this);
						var title = entry.find('title').text();
						var link = entry.find('link').text();
						var description = entry.find('description').text();
						blogTeaserContent = blogTeaserContent + '<li><a href="' + link + '" target="_blank">' + title + '</a>' + "</li>\n";
						//console.log(blogTeaserContent);
					}
					entryCounter++;
				});
				
				//jQuery('#'+containerId).children('ul').removeAttr('style');
				jQuery('#'+containerId).children('ul').html(blogTeaserContent);
			});
			
			//console.log(feedUrl);
		},
		
		initNuggetBoxes: function() {
			var boxes = jQuery(".nuggetBox");
			jQuery(".nuggetBox:first-child").css('width', '310px').addClass('big');
			//jQuery(".nuggetBox:not(.big) .nuggetBoxInner .nuggetImg").css('background-image', 'url('+jQuery(this).attr('rev')+')');
			jQuery(".nuggetImg", ".nuggetBox:not(.big)").css('background-image', function() { 
			    return 'url('+ jQuery(this).attr('rev') +')';
			});
			
			
			

			boxes.bind("mouseenter", function() { 

				var box = jQuery(this);
				
				var otherBoxes = boxes.not(box);
				if( !otherBoxes.is(':animated') ) {
					otherBoxes.animate({width:162},500, function() {
						jQuery(this).removeClass("big");
						var boxImg = jQuery(this).children('.nuggetBoxInner').children('.nuggetImg');
						boxImg.css('background-image', 'url('+boxImg.attr('rev')+')');
					});
				
					var boxImgOpen = box.children('.nuggetBoxInner').children('.nuggetImg');
					boxImgOpen.css('background-image', 'url('+boxImgOpen.attr('rel')+')');
					box.addClass("big");
					
					box.stop().animate({width:310},500);
				}
				});
			
		},
		
		webcamImages: [],
		contentWebcamImages: '',
		btnPrev: '',
		btnNext: ''
	},
	checkContentHeight: function() {
		var sidebarDefVal = 300;
		if( jQuery('#sidebar').height() < sidebarDefVal ) {
			jQuery('#sidebar').height(sidebarDefVal+'px');
		}
		
		var sidebarHeight = jQuery('#containerSidebar').height() - 400;
		var contentHeight = jQuery('#content').height();
		
		if( sidebarHeight > contentHeight ) {
			jQuery('#content').height(sidebarHeight+'px');
		}
	},
	
	newsSingleImgFade: function() {
		jQuery('#newsImgSliderList').innerfade({ speed: 'slow', timeout: 4000, type: 'sequence', containerheight: '250px' }); 
	}
};
