/**
 * Bootstrap.js - Bootstrap for all Javascript functionality
 * 
 * @author  Webstores <info at webstores dot nl>
 *           Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */

$(document).ready(function(){
	
	// External links
	$('a[rel="external"]').each(function(){this.target='_blank'});
	
	// Toggle input values
	$('.toggle-value').each(function(){var v=this.value;$(this).focus(function(){if(this.value==v){this.value=''}$(this).addClass('focus');});$(this).blur(function(){if(this.value==''){this.value=v;$(this).removeClass('focus');}});});
	
	// IE6 hover
	//if(/msie 6/i.test(navigator.userAgent)){$('#navigation li').each(function(){$(this).mouseover(function(){$(this).addClass('iehover');});$(this).mouseout(function(){$(this).removeClass('iehover');});});}

	// News ajax
	if($('#spotlight-item')) {
		if($('.news-link').length>1) {
			$('.news-link').each(function() {
				newsClickEvent(this);
			});
		}
	}
	
	// Spotlight scroller
	if($('#spotlight-scroller img').length>1) {$('#spotlight-scroller').scrollable({size:1,disabledClass:'hidden',clickable:false,prev:'#spotlight-prev',next:'#spotlight-next'});}else{$('#spotlight-item .thumb .carousel-control').addClass('hidden');}
	
	// News scroller
	if($('#news-scroller .item').length>3){$('#news-scroller').scrollable({size:3,disabledClass:'hidden',clickable:false,prevPage:'#news-prev',nextPage:'#news-next'});}else{$('#news-scroller-wrap .carousel-control').addClass('hidden');}
	
	// Brochure aanvragen textveranderen
	$('#we-are-body .more').click(function() {
		$('#contact-text').html('<h3>Ook in vervoering raken?</h3><p>Neem gerust contact op. Bij voorkeur doen we dat in een persoonlijk gesprek. Wilt u onze brochure ontvangen vul dan onderstaand formulier in.</p><p>* Uw gegevens zal alleen door Zone2design worden gebruikt en wordt niet doorgegeven aan derden.</p>');
		Cufon.replace('#contact-text h3',{fontFamily:'NeoSansStd',fontWeight:300,hover:true});
	});
	
	// Work scroller
	if($('#work-scroller img').length>1){$('#work-scroller').scrollable({size:1,disabledClass:'hidden',clickable:false,prev:'#work-prev',next:'#work-next'});}else{$('#work-scroller-wrap .carousel-control').addClass('hidden');}
	
	// Employee toggler
	$('.employee-toggler').toggler('.employee');

	// Page scroller
	$('.navigation-home a, .top, #we-are-body .more').click(function(e){e.preventDefault();scrollToAnchor(this.hash)});
	if(window.location.hash) {
		scrollToAnchor(window.location.hash);
	}
		
	if($('#references').length){references.initialize();}
	
	$("#contact-form").validate();
});

var scrollToAnchor = function(item){$('html,body').animate({scrollTop:($(item).offset().top-150)});}

var references = {
	
	expanded: false,
	currentId: -1,
	
	initialize: function() {
		
		var links = $('#references .expand-item');
		
		links.each(function(link) {
			$(this).click(function() {
				references.expand(this);				
				return false;
			});
		});
	},
	
	expand: function(link) {
		
		var row = $(link).closest('.overview-row');
		var referenceId = link.hash.split('-')[1];
		
		if(referenceId == references.currentId) {
			return false;
		}
		
		if(references.expanded) {
			if(row[0].id == $('#expanded-item-wrap').prev()[0].id) {
				$('#loading-overlay').fadeIn();
				
				$.ajax({url: getUrlProtocol() + 'zone2design.nl/references/ajax/' + referenceId, type: 'post', success: function(response) {
					references.parseResponse(response);
					
					// scroll
					scrollToAnchor('#expanded-item');
				}});
				//$.ajax({ type: 'post', url: 'portfoliodata', data: 'id=' + referenceId, succes: function(response) { references.parseResponse(response); }});
				// TEMPORARY
				
			}
			else {
				$.ajax({url: getUrlProtocol() + 'zone2design.nl/references/ajax/' + referenceId, type: 'post', success: function(response) {
					references.replaceContainer(row, response);
				}});
				
				//$.ajax({ type: 'post', url: 'portfoliodata', data: 'id=' + referenceId, succes: function(response) { references.parseResponse(response); }});
				// TEMPORARY
				//references.parseResponse(null);
			}
		}
		else {
			references.createContainer(row);
			
			$.ajax({url: getUrlProtocol() + 'zone2design.nl/references/ajax/' + referenceId, type: 'post', success: function(response) {
				references.parseResponse(response);
			}});
					
			//$.ajax({ type: 'post', url: 'portfoliodata', data: 'id=' + referenceId, succes: function(response) { references.parseResponse(response); }});
			// TEMPORARY
			references.parseResponse(null);
		}		
		
		references.currentId = referenceId;
	},
	
	createContainer: function(row) {
		
		var containerWrap = document.createElement('div');
		containerWrap.id = 'expanded-item-wrap';
		containerWrap.style.display = 'none';
		
		var container = document.createElement('div');
		container.id = 'expanded-item';
		
		containerWrap.appendChild(container);
		row.after(containerWrap);
		
		$(containerWrap).slideDown();
		
		// scroll
		scrollToAnchor('#expanded-item');
		
		return container;
	},
	
	replaceContainer: function(row, response) {
		$('#expanded-item-wrap').slideUp(function() {
			$(this).remove();
			references.createContainer(row);
			references.parseResponse(response);
		});		
		
		references.expanded = false;
		references.currentId = -1;		
	},
	
	removeContainer: function() {
		
		$('#expanded-item-wrap').slideUp(function() {
			$(this).remove();
			
		});	
		
		references.expanded = false;
		references.currentId = -1;
	},
	
	parseResponse: function(response) {
		
		// TEMPORARY
		//response = '<div id="item-info"><div id="item-thumbs" class="clear"><a class="thumb" href="#" title="Exapol thumb 1"><img src="img/expanded-item-thumb-1.jpg" alt="Exapol identity 1" width="50" height="50"></a><a class="thumb" href="#" title="Exapol thumb 2"><img src="img/expanded-item-thumb-2.jpg" alt="Exapol identity 2" width="50" height="50"></a><a class="thumb" href="#" title="Exapol thumb 3"><img src="img/expanded-item-thumb-3.jpg" alt="Exapol identity 3" width="50" height="50"></a><a class="thumb" href="#" title="Exapol thumb 4"><img src="img/expanded-item-thumb-4.jpg" alt="Exapol identity 4" width="50" height="50"></a></div><h4>Exapol identity</h4><p>Ip erciliquat amcommolor si tatisisi tis niat. Ut ipit nisi blaor ipsummy nibh exero dolore conse venis <a id="expanded-item-close" href="#" title="Sluiten">nosto</a>.</p></div><div id="item-scroller"><div id="item-content"><img src="img/expanded-item-1.jpg" alt="Exapol identity 1" width="528" height="326"><img src="img/expanded-item-2.jpg" alt="Exapol identity 2" width="528" height="326"><img src="img/expanded-item-3.jpg" alt="Exapol identity 3" width="528" height="326"><img src="img/expanded-item-4.jpg" alt="Exapol identity 4" width="528" height="326"></div></div><div id="loading-overlay"></div><script type="text/javascript">if($(\'#item-scroller img\').length>1){$(\'#item-scroller\').scrollable({speed:800,size:1,loop:true}).autoscroll({interval:5000}).navigator({navi:\'#item-thumbs\',activeClass:\'selected\'});}</script>';
		
		$('#expanded-item').html(response);
		Cufon.replace('#expanded-item h4',{fontFamily:'NeoSansStd',fontWeight:300,hover:true});
		
		references.attachClose();
		references.expanded = true;
		
		$('#loading-overlay').fadeOut();
	},
	
	attachClose: function() {
		
		$('#expanded-item-close').click(function() {
			references.removeContainer();
			return false;
		});
	}
};

function newsClickEvent(self) {
	var newsId = $(self).parents('.item')[0].id.split('-')[1];
	$(self).click(function() {		
		$.ajax({url: getUrlProtocol() + 'zone2design.nl/news/ajax_title/' + newsId, type: 'post', success: function(response) {
		    $('#spotlight-meta').html(response);
		}});
		$.ajax({url: getUrlProtocol() + 'zone2design.nl/news/ajax/' + newsId, type: 'post', success: function(response) {
		    $('#spotlight-item').html(response);
		    if($('#spotlight-scroller img').length>1) {$('#spotlight-scroller').scrollable({size:1,disabledClass:'hidden',clickable:false,prev:'#spotlight-prev',next:'#spotlight-next'});}else{$('#spotlight-item .thumb .carousel-control').addClass('hidden');}		    
		    Cufon.replace('#spotlight-item h3',{fontFamily:'NeoSansStd',fontWeight:400,hover:true});
		}});
		
		var id = $('#spotlight-item :input')[0].value;
		$.ajax({url: getUrlProtocol() + 'zone2design.nl/news/ajaxsmall/' + id, type: 'post', success: function(response) {
			if($('#news-' + newsId).parents('#sidebar-news').length > 0) {
				var response = response.replace('/small/', '/homepage_small/');
			}
			$('#news-' + newsId).replaceWith(response);			
			$('#news-' + id).find('.news-link').each(function() {
				newsClickEvent(this);
			});			
		    Cufon.replace('h4',{fontFamily:'NeoSansStd',fontWeight:300,hover:true});		    
		}});
	});
}

function getUrlProtocol() {
	if(location.href.search('http://www.') != -1) {
		return 'http://www.';
	} else {
		return 'http://';
	}
}

