﻿var MODE_EDIT = window.location.href.match(/PHPSESSID/i);

jQuery(document).ready(function() {
	
	// clear input fields
	jQuery('input').each(function() {
		jQuery(this).click(function() {
			if(Search.inputFields[jQuery(this).index()]==undefined || 
				 jQuery(this).val()==Search.inputFields[jQuery(this).index()]) {
				Search.inputFields[jQuery(this).index()] = jQuery(this).val();
				jQuery(this).val('');
			}
		});
		jQuery(this).blur(function() {
			if(jQuery(this).val() == '')
				jQuery(this).val(Search.inputFields[jQuery(this).index()]);
		});
	});
	
	// reasamble tagcloud
	jQuery('div.TagCloud').each(function() {
		TagCloud.initialize(jQuery(this));
	});
	
	// fancy iPad Elements
	jQuery('div.Inhaltsbox > div').each(function() {
		if(jQuery(this).width() > 700) {
			if(!jQuery(this).hasClass('full')) {
				jQuery(this).removeAttr('style');
				jQuery(this).css({
					'padding-bottom': '10px',
					'height': 'auto'
				});
			}
		}
	});
	
	// kill all styles for handhelds
	if(document.width <= 350 || jQuery(document).width() < 350)
		jQuery('div.Inhaltsbox > div').attr('style', '');
		
	// target: _blank
	jQuery('div.bodytext a').click(function() {
		var href = jQuery(this).attr('href');
		window.open(href);
		return false;
	});
	
	if(MODE_EDIT == null) {
		// eyecatcher cycle
		jQuery('.Eyecatcher div.highres').cycle('fade');
		jQuery('.Eyecatcher div.lowres').cycle('fade');
	}
	
	// show anymation for IE8-
	if(document.width > 750 || jQuery(document).width() > 750)
		jQuery('.Eyecatcher div.highres').show();
	else
		jQuery('.Eyecatcher div.lowres').show();
		
	// remove value
	jQuery('input#btD21Search').val('');
	
	// clickable tiles
	jQuery('div.Inhaltsbox > div').click(function() {
		var href = jQuery(this).find('a.engine').attr('href');
		if(undefined != href && href != '')
			window.location.href = href;
	});
});

// Images
function resizeImages() {
	if(document.width > 350 || jQuery(document).width() > 350) {
		// image format
		jQuery('div.Inhaltsbox > div div.imagewrap img').each(function() {
			var parentHeight = jQuery(this).parent().height();
			var parentWidth = jQuery(this).parent().width();
			if(parentWidth > parentHeight) {
				// Container: Querformat
				if(jQuery(this).width() < jQuery(this).height()) {
					// Bild: Hochformat
					jQuery(this).css({'width' : '100%', 'height' : 'auto'});
				} else {
					// Bild: Querformat
					jQuery(this).css({'height' : '100%', 'width' : 'auto'});
				}
				// Nachjustieren
				if(jQuery(this).width() < parentWidth) {
					jQuery(this).css({'width' : '100%', 'height' : 'auto'});
				}
			} else {
				// Container: Hochformat
				if(jQuery(this).width() < jQuery(this).height()) {
					// Bild: Hochformat
					jQuery(this).css({'width' : '100%', 'height' : 'auto'});
				} else {
					// Bild: Querformat
					jQuery(this).css({'height' : '100%', 'width' : 'auto'});
				}
				// Nachjustieren
				if(jQuery(this).height() < parentHeight) {
					jQuery(this).css('height', '100%');
					jQuery(this).css('width', 'auto');
				}
			}
		});
	}
}
jQuery(window).load(function() {
	resizeImages();
	// fancybox
	if(MODE_EDIT == null)
		jQuery('div.images a').fancybox();
});

var Search = {
	inputFields : []
};

var TagCloud = {
	container : {},
	keywords : [],
	initialize : function(container) {
		this.reset();
		this.container = container;
		jQuery(this.container).find('p.keyword').each(function() {
			TagCloud.keywords.push(new Keyword(
				jQuery.trim(jQuery(this).find('span.word').text()),
				parseInt(jQuery(this).find('span.ranking').text()),
				jQuery(this)
			));
		});
		this.sortKeywords();
		this.reassemble();
	},
	reset : function () {
		this.container = {};
		this.keywords = [];
	},
	sortKeywords : function() {
		var tempArray = this.keywords;
		for(var x=0;x<tempArray.length*tempArray.length;x++) {
			for(var i=0;i<tempArray.length;i++) {
				if(i < (tempArray.length-1))
					var j = i+1;
				else
					var j = i;
				if(tempArray[i].percentage<tempArray[j].percentage) {
					var tempWord = tempArray[i];
					tempArray[i] = tempArray[j];
					tempArray[j] = tempWord;
				}
			}
		}
		this.keywords = tempArray;
	},
	reassemble : function() {
		jQuery(this.container).find('p.keyword').css({
			'position': 'absolute', 
			'display': 'block',
			'width': 'auto'
		});
		for(var i=0;i<this.keywords.length;i++) {
			if(i==0) {
				jQuery(this.keywords[i].domReference).css({
					'top': '48%',
					'left': '48%',
					'z-index': 1
				});
				var marginLeft = -1 * (jQuery(this.keywords[i].domReference).width()/2);
				jQuery(this.keywords[i].domReference).css({
					'margin-left': marginLeft + 30
				});
			} else {
				var top = (Math.floor(Math.random()*100)%80) + '%';
				while(top<20) {
					top = (Math.floor(Math.random()*100)%80) + '%';
				}
				var left = (Math.floor(Math.random()*100)%80) + '%';
				while(left<20) {
					left = (Math.floor(Math.random()*100)%80) + '%';
				}
				var tone = (Math.floor(Math.random()*100)%6);
				if(tone == 0)
					tone = '#dbc4ab';
				else if(tone == 1)
					tone = '#c9a781';
				else if(tone >= 2)
					tone = '#B78957';
				jQuery(this.keywords[i].domReference).css({
					'top': top,
					'left': left,
					'color': tone,
					'z-index': 0
				});
			}
		}
	}
};

var Weather = {
	location : 'Oberkirch',
	weatherData : {},
	initialize : function(location) {
		this.location = location;
		jQuery.ajax({
			url : 'http://www.google.com/ig/api',
			data : {weather : this.location, hl : 'de'},
			success : function(msg) {
			},
			dataType : 'xml'
		});
	},
	fahrenheitToCelsius : function(fahrenheit) {
		return fahrenheit/1.8 - 32;
	}
}

// keyword object
function Keyword (keyword, percentage, domReference) {
	this.percentage = percentage;
	this.keyword = keyword.replace(/(\r\n|\n|\r)/gm,"");
	this.domReference = domReference;
};

// WeatherData object
function WeatherData (condition, temperature) {
	this.condition = condition;
	this.temperature = tempereature;
}
