
var emailDefault = "E-Mail Adresse";
var slideshowTimeout = null;
var slidingEnabled = true;
var slidingInterval = 8000;
var fadeDuration = 750;
var currentParms = new Array();

$(document).ready(function() {
	$('#newsletterEmail').val(emailDefault);
	$('#newsletterEmail').bind({
		focus: function() {
			if($(this).val() == emailDefault) $(this).val("");
			$(this).addClass("focus");
		},
		blur: function() {
			if($(this).val() == "") {
				$(this).val(emailDefault);
				$(this).removeClass("focus");
			}
		}
	});
});

function slide(id) {
	setTimeout(function() {
		$('#' + id).slideDown({ duration: 600 });
	}, 400);
}

function slideshow(target) {
	if(!slidingEnabled) return false;
	slidingEnabled = false;
	if(slideshowTimeout != null) clearTimeout(slideshowTimeout);
	var slides = $('#slideshow').children('.slideItem');
	for(var i = 0; i < slides.length; i ++) {
		if($(slides[i]).attr("class").indexOf("first") > -1) {
			if(target == null) var next = (i +1) >= slides.length ? 0 : i +1;
			else {
				var parts = target.split("_");
				var next = parseInt(parts[1]);
			}
			$('#page_' + i).bind('click', function() {
				slideshow($(this).attr("id"));
			});
			$('#page_' + next).unbind('click');
			$('#page_' + i).removeClass("active");
			$('#page_' + next).addClass("active");
			$(slides[i]).fadeOut(fadeDuration, function() {
				$(this).removeClass("first");
			});
			$(slides[next]).fadeIn(fadeDuration, function() {
				$(this).addClass("first");
				slidingEnabled = true;
				slideshowTimeout = setTimeout(function() {
					slideshow();
				}, slidingInterval);
			});
		}
	}
}

function createSlideshow(data) {
	var count = 0;
	var items = new Array();
	for(var i = 0; i < data.length; i ++) {
		if(!data[i].images) continue;
		var images = data[i].images.split(",");
		var imageHtml = slideImageHtml.replace(/##IMG##/g, images[0]);
		var itemHtml = slideItemHtml.replace(/##IMAGE##/g, imageHtml);
		itemHtml = itemHtml.replace(/##TITLE##/g, data[i].title);
		if(count == 0) itemHtml = itemHtml.replace(/##CLASS##/g, " first");
		else itemHtml = itemHtml.replace(/##CLASS##/g, "");
		items.push(itemHtml);
		count ++;
	}
	var html = slideShowHtml.replace(/##ITEMS##/g, items.join(""));
	var pages = "";
	for(var j = 0; j < count; j ++) {
		if((j +1) >= count) pages += "<div class=\"slidePage active\" id=\"page_" + (count - j -1) + "\"></div>";
		else pages += "<div class=\"slidePage\" id=\"page_" + (count - j -1) + "\"></div>";
	}
	html = html.replace(/##PAGES##/g, pages);
	$('#contentBody').append(html);
	var pages = $('#slidePaging').children('.slidePage');
	for(var k = 0; k < pages.length; k ++) {
		if($(pages[k]).attr("class").indexOf("active") < 0) {
			$(pages[k]).bind('click', function() {
				slideshow($(this).attr("id"));
			});
		}
	}
	slideshowTimeout = setTimeout(function() {
		slideshow();
	}, slidingInterval);
}

function createFullsizeTeaser(data) {
	var html = fullsizeHtml.replace(/##TITLE##/g, data.title);
	html = html.replace(/##DATE##/g, data.hrDate);
	html = html.replace(/##SHORT##/g, data.short);
	html = html.replace(/##TEXT##/g, data.text);
	html = html.replace(/##LINK##/g, 'projekte/#/kategorie/0/projekt/' + data.article_id);
	var images = data.images.split(",");
	html = html.replace(/##IMG##/g, images[0]);
	$('#contentBody').append(html);
}

function createHalfsizeTeaser(data) {
	for(var i = 0; i < data.length; i ++) {
		var html = halfsizeHtml.replace(/##TITLE##/g, data[i].title);
		html = html.replace(/##DATE##/g, data[i].hrDate);
		html = html.replace(/##SHORT##/g, data[i].short);
		html = html.replace(/##TEXT##/g, data[i].text);
		var images = data[i].images.split(",");
		html = html.replace(/##IMG##/g, images[0]);
		$('#contentBody').append(html);
	}
	$('#contentBody').append("<br class=\"clear\">");
}

function createProjectOverview(data) {
	projects = data.getProjects.projects;
	$.each($('.loading'), function() {
		$(this).remove();
	});
	$('#contentBody').append($('<div>').attr({ id: 'teaserContainer' }));
	var rowNum = 0;
	for(var i = 0; i < projects.length; i ++) {
		if(i %2 == 0) {
			rowId = 'row_' + (i /2);
			$('#teaserContainer').append($('<div>').attr({ id: rowId }).addClass('rowContainer'));
		}
		
		var html = overviewHtml.replace(/##TITLE##/g, projects[i].title);
		var images = projects[i].images.split(",");
		html = html.replace(/##IMG##/g, images[0]);
		html = html.replace(/##TEXT##/g, projects[i].short);
		var icons = "";
		for(var j = 0; j < 7; j ++) {
			if(j == 3) continue;
			if(projects[i]['c' + j]) {
				icons += '<img src="static/img/icons/24x23_c' + (j) + '.png" width="24" height="23" alt="Kategorie Icon">';
			}
		}
		html = html.replace(/##ICONS##/g, icons);
		html = html.replace(/##LINK##/g, 'javascript:void(0)" onclick="javascript:projectDetail(' + cats[projects[i]['maincat']] + ', ' + projects[i]['article_id'] + ')');
		$('#' + rowId).append(html);
		//$('#teaserContainer').append(html);
	}
	
	$('#teaserContainer').fadeIn();
	$('#paging').html('');
	if(data.getProjects.pageCount > 1) {
		var pages = '';
		for(var k = data.getProjects.pageCount -1; k > -1; k --) {
			if((k +1) == data.getProjects.currentPage) $('#paging').append($('<div>').attr({ id: 'page_' + (k +1) }).addClass('slidePage active'));
			else {
				$('#paging').append($('<div>').attr({ id: 'page_' + (k +1) }).addClass('slidePage').click(function() { 
					//prepareProjectOverview(data.getProjects.cids, $(this).attr('id').split('page_').join('')); 
					updateDeeplink(getAjaxParam('kategorie'), null, $(this).attr('id').split('page_').join(''));
				}));
			}
		}
		$('#paging').css('display', 'block');
	}
	else {
		$('#paging').html('').css('display', 'none');
	}
	//if(data.getProjects['cids'].indexOf(',') == -1) changeBreadcrumb(data.breadcrumbCategory);
	changeBreadcrumb(data.breadcrumbCategory);
	resized();
}

function projectDetail(category, project) {
	$('#teaserContainer').remove();
	$('#paging').html('');
	updateDeeplink(category, project, false);
}

function fileExists(url) {
	$.ajax({
			url:url,
			type:'HEAD',
			error:
					function(){
							//do something depressing
							return false;
					},
			success:
					function(){
							//do something cheerful :)
							return true;
					}
	});
}

function createProjectDetail(data) {
	$('#contentBody').append('<div id="teaserContainer"></div>');
	project = data.getProject;
	$.each($('.loading'), function() {
		$(this).remove();
	});
	console.log(project.title);
	interpretPagetitle(project.title);
	var html = projectHtml.replace(/##TITLE##/g, project.title);
	html = html.replace(/##DATE##/g, project.hrDate);
	html = html.replace(/##SHORT##/g, project.short);
	html = html.replace(/##TEXT##/g, project.text);
	html = html.replace(/redaxo:\/\//g, 'index.php?article_id=');

	if(project.video) {
		var id = project.video.split("/");
		id = id[id.length-1];
		var video =  vimeoEmbed.replace(/##vID##/g, id);
		html = html.replace(/##SLIDESHOW##/g, video);
	} else {
		var images = project.images.split(",");
		if(images.length == 1) {
			var image =  slideImageHtml.replace(/##IMG##/g, images[0]);
			html = html.replace(/##SLIDESHOW##/g, image);
		}
		else {
			var count = 0;
			var items = "";
			for(k = 0; k < images.length; k ++) {
				if(!images[k]) continue;
				var imageHtml = slideImageHtml.replace(/##IMG##/g, images[k]);
				var itemHtml = slideItemHtml.replace(/##IMAGE##/g, imageHtml);
				if(count == 0) itemHtml = itemHtml.replace(/##CLASS##/g, " first");
				else itemHtml = itemHtml.replace(/##CLASS##/g, "");
				items += itemHtml;
				count ++;
			}
		}
	}
	var pages = "";
	for(j = 0; j < count; j ++) {
		if((j +1) >= count) pages += "<div class=\"slidePage active\" id=\"page_" + (count - j -1) + "\"></div>";
		else pages += "<div class=\"slidePage\" id=\"page_" + (count - j -1) + "\"></div>";
	}
	var slideshow = slideShowHtml.replace(/##ITEMS##/g, items);
	slideshow = slideshow.replace(/##PAGES##/, pages);
	slideshow += "<script>	var pages = $('#slidePaging').children('.slidePage');	for(var k = 0; k < pages.length; k ++) {		if($(pages[k]).attr(\"class\").indexOf(\"active\") < 0) {			$(pages[k]).bind('click', function() {				slideshow($(this).attr(\"id\"));			});		}	}	slideshowTimeout = setTimeout(function() {		slideshow();	}, slidingInterval);	</script>";
	
	html = html.replace(/##SLIDESHOW##/g, slideshow);
	
	$('#teaserContainer').append(html);
	$('#bigIcons').append($('<div>').attr({ id: 'projectIcons' }).addClass('top50'));
	for(var j = 0; j < 7; j ++) {
		if(j == 3) continue;
		if(project['c' + j]) {
			//if(fileExists("static/img/icons/36x35_c' + (j) + '.png")) {
				$('#projectIcons').append('<img src="static/img/icons/36x35_c' + (j) + '.png" width="36" height="35" alt="Kategorie Icon">');
			//}
		}
	}

	$('#teaserContainer').fadeIn();
	switch(project['cat_name']) {
		case 'Alle':
		var pid = 0;
		break
		case 'Online Werbung':
		var pid = 1;
		break;
		case 'Social Media':
		var pid = 2;
		break;
		case 'Mobile':
		var pid = 3;
		break;
		case 'Cross Media':
		var pid = 4;
		break;
	}
	changeBreadcrumb(project['cat_name'], project['title'],pid );
}

function changeSubnavigation(subId, activeId, changeBc) {
	//console.log(subId+" "+ activeId+" "+ changeBc)
	$.each($('#subNavigation_' + subId + ' .subnavigationItem'), function() {
		$(this).removeClass('active');
	});
	$.each($('#subNavigation_' + subId + ' .subnavImage'), function() {
		$(this).attr({ src: $(this).attr('src').replace(/-active/g, '') });
	});
	
	$('#subNavigation_' + subId + ' #subnavigationItem_' + activeId).addClass('active');
	var img = $('#subNavigation_' + subId + ' #subnavImage_' + activeId);
	if($(img).attr('id') == 'subnavImage_' + activeId) $(img).attr({ src: $(img).attr('src').replace(/subnavicon/g, 'subnavicon-active') });
	
}

function changeBreadcrumb(category, project, categoryId) {
	if(project != undefined) {
		interpretPagetitle(project);
	} else {
		interpretPagetitle(category);
	}
	var bci = $('<div>').addClass('breadCrumbItem');
	var navlink = $('<a>').attr({ href: 'index.php?article_id=6#/kategorie/0', title: 'Projekte' }).html('Projekte');
	var slash = $('<span>').addClass('regular').html(' / ');
	var slash2 = $('<span>').addClass('regular').html(' / ');
	$(bci).append(navlink);
	if(category != undefined) $(bci).append(slash);
	if(project != undefined) {
		var catlink = $('<a>').attr({ href: 'javascript:void(0)', title: '#' }).html(category).click(function() {
			updateDeeplink(categoryId);
		});
		$(bci).append(catlink);
		$(bci).append(slash2);
		$(bci).append(project);
	}
	else {
		$(bci).append(category);
	}
	$('#breadCrumb').html(bci);
}

function resized() {
	if($.browser.msie) return false;
	$('#footer').css('margin-top', '35px');
	var pos = $('#footer').position();
	var documentHeight = $(document).height();
	var diff = documentHeight - (pos.top + $('#footer').height());
	$('#footer').css('margin-top', '0px');
	if(diff > 0) {
		$('#footer').css('margin-top', diff + 'px');
	}
}

function updateDeeplink(category, project, page) {
	if(page == undefined) page = '1';
	currentLocation = String(window.location).split('#');
	var newLocation = currentLocation[0] + '#/kategorie/' + category;
	if(project != undefined) newLocation += '/projekt/' + project;
	if(page != false) newLocation += '/seite/' + page;
	window.location = newLocation;
	interpretDeeplink();
}

function interpretDeeplink() {
	currentLocation = String(window.location).split('#');
	parms = currentLocation[1] == undefined ? null : currentLocation[1].substr(1).split('/');
	if(parms == null) {
		updateDeeplink(0);
		return false;
	}
	$('#projectIcons').remove();
	var data = new Array();
	for(var i = 0; i < parms.length; i += 2) {
		data[parms[i]] = parms[i +1];
	}
	changeSubnavigation(5, data['kategorie']);
	if(data['projekt'] == undefined) {
		
		prepareProjectOverview(data['kategorie'], data['seite']);
	}
	else loadProject(data['projekt']);
}

function interpretPagetitle(category) {
	document.title = category + ' - TUNNEL23.COM';
}

function getAjaxParam(name) {
	currentLocation = String(window.location).split('#');
	parms = currentLocation[1] == undefined ? null : currentLocation[1].substr(1).split('/');
	if(parms == null) return false;
	var data = new Array();
	for(var i = 0; i < parms.length; i += 2) {
		data[parms[i]] = parms[i +1];
	}
	if(data[name] == undefined) return false;
	return data[name];
}



