/** 
 * walther-expointerieur.de - Project JS Functions *
 * @author Michael Betka
 * @version 1.0 
 * @see http://www.walther-expointerieur.de/
 */

var projectDetailPopup = false;
var ieLess8 = true;
/**
 * assign the accordeon effect for the main navigation
 */
$(document).ready(function(){
	$('body').removeClass('noJSBody');
	$('body').addClass('bodybgclass1');
	ieLess8 = $.browser.msie && ($.browser.version<8.0);
	$('#navWrap h2 a').each(function(){
		if(this.rel!='' && !$(this).parent().hasClass('navactH2')){
			jQuery.getJSON('/templates/jquery/standard_content.php', {topic:this.rel}, navigationResult);
		}
		$(this).unbind('click');
		$(this).click(function(){
			toggleMainNavigation(this);
			return false;
		});
	});
	assignGalleryHandler( projectDetailPopup );
	assignHomepageSlider(  );
	fixResize();
});
$(window).resize(
	function(){
		fixResize();
	}
);
function fixResize(){
	$('.galeryWrap').each(function(){
		var outerBox = ($(this).parent().parent().parent().parent().width()-2);
		if(outerBox<940){
			outerBox = 940;
		}
		$(this).parent().parent().parent().width( outerBox + 'px');
		
		var isDetailOverview = false;
		var vPix = 965;
		if( $(this).parent().parent().parent().attr('class') == 'detailOverlay' ){
			vPix = $(this).parent().parent().parent().width()-160-130;
			isDetailOverview = true;
		}else{
			vPix = $(this).parent().parent().width()-160;
			isDetailOverview = false;
		}
		if(vPix>965){
			vPix = 965;
		}
		if(vPix<650){
			vPix = 650;
		}
		if(isDetailOverview){
			$(this).width(vPix + 'px');
			$(this).parent().width(vPix + 'px');
		}else{
			$(this).width(vPix + 'px');
			$(this).parent().width(vPix + 'px');
		}
	});
}

/**
 * assign the contents for the navigation
 */
function navigationResult( result ){
	$('#' + result.topic).html(result.content);
	
	refreshSubNav();

	fixResize();
}

function subNavigationResult( result ){
	$('#' + result.topic).html(result.content);
	$('#' + result.topic).find('.jQsubnav').remove();
	//$('#' + result.topic).html($('#' + result.topic + ' div:first-child').html());
	assignGalleryHandler( $('#' + result.topic) );
	//assignGalleryHandler( $('#' + result.newtopic) );
	assignProjectHandler( $('#' + result.topic) );
	//assignProjectHandler( $('#' + result.newtopic) );
	
	$('a.dbdetail').each(function(){
		if( $(this).attr('rel').indexOf('walther_projects',0)>=0 ){
			$(this).unbind('click');
			$(this).click(function(){toggleProjectDetailShow(this);return false;});
		}
	});
	
	fixResize();
	fixResize();
}

/**
 * executed if main navigation item is clicked
 */
function toggleMainNavigation( clicked ){
	if(!$(clicked).parent().hasClass('navactH2')){
		$('#navWrap h2.navactH2 a').each(function(){$(this).parent().removeClass('navactH2');$('#' + this.rel).slideUp(150);});
		$('#navWrap #' + clicked.rel).slideDown(150);
		$(clicked).parent().addClass('navactH2');
	}
	fixResize();
}

function toggleSubNavigation( clicked ){
	if($(clicked).parent().find('a.act').length==0){
		if(ieLess8){
			$('#'+clicked.rel).css('display','block');
		}else{
			$('#'+clicked.rel).animate({height: 'show'},function(){if($.browser.safari) $(this).css('opacity','0.99');});//show();
		}
		fixResize();
	}
	$(clicked).parent().find('a').each(
		function(){
			if($(this).hasClass('act') && clicked != this){
				if( ieLess8 ){
					$('#'+this.rel).css('display','none');
					if( $('#'+clicked.rel).css('display')!='block' ){
						$('#'+clicked.rel).css('display','block');
					}
					fixResize();
				}else{
					$('#'+this.rel).animate({height: 'hide'},function(){
						if( $('#'+clicked.rel).css('display')!='block' ){
							$('#'+clicked.rel).animate({height: 'show'},function(){if($.browser.safari) $(this).css('opacity','0.99');});//show();
							fixResize();
						}
					});
				}
			}
			
			$(this).removeClass('act');
		}
	);
	$(clicked).addClass('act');
}

/**
 * Detailseite eines Projektes aufrufen
 */
function toggleProjectDetailShow( clicked ){
	var mainParent = $(clicked).parent().parent().parent().parent().parent().parent();
	var contentDiv = mainParent.children(":first");
	if( contentDiv.attr('class') != 'detailOverlay' ){
		var h = $(contentDiv).parent().height();
		if(h<340){
			h = 340;
		}
		contentDiv.before('<div class="detailOverlay" style="display:none;height:' + h + 'px"></div>');
	}
	projectDetailPopup = mainParent.children(":first");
	jQuery.getJSON('/templates/jquery/standard_project_db.php', {data:clicked.rel}, toggleProjectDetailShowResult);
	return false;
}

function toggleProjectDetailShowResult( result ){
	projectDetailPopup.html( result.content );
	
	projectDetailPopup.fadeIn();
	
	$('.jClose',projectDetailPopup).each(function(){
		$(this).unbind('click');
	});
	
	$('.jClose',projectDetailPopup).click(toggleProjectDetailClose);
	
	assignGalleryHandler( projectDetailPopup );
	
	fixResize();
}

/**
 * Detailseite eines Projektes schließen
 */
function toggleProjectDetailClose(){
	if(projectDetailPopup!=false){
		$(projectDetailPopup).fadeOut();
	}
	return false;
}

function refreshSubNav(){
	var matchedElement = false;
	$('div.jQsubnav').each(function(){
		if( $(this).next()){
			if($(this).next().attr('id')=='' || $(this).next().attr('id')=='undefined'){
				$(this).next().css('display','none');
				matchedElement = $(this).next();
			}
		}
	});
	$('div.jQsubnav a').each(function(){
		if( this.rel.indexOf('tyNavigationTopicID',0)>=0 ){
			$(this).unbind('click');
			$(this).click(function(){toggleSubNavigation(this);return false;});
			if( $('#'+this.rel).length == 0 ){
				$(this).parent().after('<div id="' + this.rel + '" class="subnavcontent" style="display:' + ($(this).hasClass('act')?'block':'none') + ';"></div>');
				if($(this).hasClass('act')){
					if( matchedElement ){
						$('#' + this.rel).append( matchedElement );
						matchedElement.css('display','block');
					}
					assignGalleryHandler($('#'+this.rel));
					assignProjectHandler($('#'+this.rel));
					// $(this).parent().parent().children().last().appendTo(  );
				}else{
					if(this.rel != '' && $('#' + this.rel).length != 0){
						jQuery.getJSON('/templates/jquery/standard_content.php', {topic:this.rel}, subNavigationResult);
					}
				}
			}
		}
	});
}

function assignProjectHandler(container){
	var prevLink = $('.pager a.prevlink',container);
	var nextLink = $('.pager a.nextlink',container);
	prevLink.rel = container.id;
	nextLink.rel = container.id;
	
	$('ul.projectlist',container).projectSlider({prevElem:prevLink,nextElem:nextLink});
}

function assignGalleryHandler(container){
	var prevLink = $('.pager a.prevlink',container);
	var nextLink = $('.pager a.nextlink',container);
	prevLink.rel = container.id;
	nextLink.rel = container.id;
	
	$('ul.galery',container).picSlider({prevElem:prevLink,nextElem:nextLink});
}

function assignHomepageSlider(){
	$('a.indexProjectsPager').unbind('click');
	$('div.indexProjectWrap div:first-child').picSlider({'nextClicked':function(nidx,pidx){$('body').removeClass('bodybgclass' + (pidx+1)); $('body').addClass('bodybgclass' + (nidx+1));},'liWidth':545, 'startOnFirst':true, nextElem:$('a.indexProjectsPager')});
	//$('a.indexProjectsPager').click(function(){return false;});
}
