$(document).ready(function() {
						   
	// enable slideshows
	$('.slided').cycle();
	
	// rebuild email addresses with ajax
	$('a.employee-mail').each(function() {
		var id = $(this).attr('rel');
		var tlink = $(this);
		$.getJSON('/agency/email/' + id, null, function(json){
			// replace anchor with email
			$(tlink).attr('href', 'mailto:' + json);
		});
	});
	
	// store current picked letter to switch back from fullview
	var lastLetterSelectionUrl = '';
	
	// model browse ajax handling
	$('a.letterselect').click(function() {
		var url = $(this).attr('href');		   
		lastLetterSelectionUrl = url;
		getModelsByLetter(url, this);
		return false;
	});
	
	$('div#model-letter-browser li.disabled a').click(function() { return false; });
	
	// bind model detail links
	rebindModelDetailLinks();
	
	// bind image view options
	rebindImageViewOptions();
	
	// toggle full view
	selectorOriginWidth = $('div#holder-model-selection').width();
	selectorOriginInnerWidth = $('div#model-list-selecter').width();
	$('#btn-toggle-fullview a').click(function() {
		// switch label
		var oldLabel = $(this).text();
		var newLabel = $(this).attr('rel');
		$(this).text(newLabel);
		$(this).attr('rel', oldLabel);
		// handle resizing
		if($('div#model-detail-holder:hidden').length > 0) {
			// close fullview
			getModelsByLetter(lastLetterSelectionUrl, this);
			$('div#holder-model-selection').width(selectorOriginWidth);												   
			$('div#model-list-selecter').width(selectorOriginInnerWidth);
			$('div#model-detail-holder:hidden').fadeIn('fast', function() {	
				return false;
			});
		} else if($('div#model-detail-holder:visible').length > 0) {
			// open fullview
			getModelsByLetter($(this).attr('href'), this);
			$('div#model-detail-holder:visible').fadeOut('fast', function() {
				$('div#holder-model-selection').width('100%');
				$('div#model-list-selecter').width('100%');
				return false;
			});
		}
		return false;
	});
	
	// init validation of forms
	var errorContainer = $('div.validation-error-container');
	$(errorContainer).hide();
	$('form.validatable').validate({
		errorContainer: errorContainer,
		errorLabelContainer: $('ol', errorContainer),
		meta: 'validate',
		onsubmit: true,
		onblur: false,
		onkeyup: false,
		wrapper: 'li'
	});
	
	// init google maps
	if (document.getElementById('city-map') && GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById('city-map'));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(53.581601688687385, 9.97107982635498), 15);
		var point = new GLatLng(53.581601688687385, 9.97107982635498);
  		map.addOverlay(createMarker(point));
	}
	
	// news ajax view
	$('div.news-item a').click(function() {
		$('div#news-detail-inner').fadeOut();
		var newsId = $(this).attr('rel') || 0;
		$('div#news-detail-inner').load('/news/entry/' + newsId, null, function() {
			$('div#news-detail-inner').fadeIn();
		});
		return false;
	});
	
	// toggle route description
	$('div#route-description').hide();
	$('h2#head-route-description').click(function() {
		$('div#route-description').slideToggle('slow');
	});
	
});

// save origin widths for model containers
var selectorOriginWidth = null;
var selectorOriginInnerWidth = null;

// create gmaps marker
function createMarker(point) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml('<img src="/images/logo_small.gif" /><!--<div style="color: #000; font-size: 1.2em;"><strong>MD Management</strong><br>Eppendorfer Weg 213<br>20253 Hamburg</div>-->');
	});
	return marker;
}

// bind model thumb to detail view by click event
function rebindModelDetailLinks() {
	$('div#model-list-selecter a, div.topmodel-item a').click(function() {
		// show indicator
		$('div#model-detail-holder').css('background', 'url(/images/roller.gif) center center no-repeat');
		// show target area													   
		$('div#holder-model-selection').width(selectorOriginWidth);
		$('div#model-list-selecter').width(selectorOriginInnerWidth);
		$('div#model-detail-holder:hidden').fadeIn('fast', function() {	
			return false;
		});
		// make ajax call
		var location = $(this).attr('href');
		$('div#model-detail-holder').empty();
		$('div#model-detail-holder').load(location, {ajax: 'true'}, function() {
			// handle add-to-cart links
			$('li.cart-adder a').click(function() {
				$(this).removeClass('enabled');
				$(this).addClass('disabled');
			});
			// init thickbox links
			tb_init('div#model-detail-holder a.thickbox');
			// hide indicator
			$('div#model-detail-holder').css('background', 'none');
			// image viewing options
			rebindImageViewOptions();
		});
		return false;
	});
}

// rebind image option handling
function rebindImageViewOptions() {
	$('div#polaroid-gallery').hide();
	$('li.navi-photos').hide();
	$('a#switch-to-photos').addClass('active');
	$('a#switch-to-photos').click(function() {
		$('li.navi-photos').hide();
		$('li.navi-polaroids').show();
		$('div#polaroid-gallery:visible').fadeOut('slow', function() {
			$('div#photo-gallery').fadeIn();
		});
		$('a#switch-to-polaroids').removeClass('active');
		$('a#switch-to-photos').addClass('active');
		return false;
	});
	$('a#switch-to-polaroids').click(function() {
		$('li.navi-photos').show();
		$('li.navi-polaroids').hide();
		$('div#photo-gallery:visible').fadeOut('slow', function() {
			$('div#polaroid-gallery').fadeIn();
		});
		$('a#switch-to-photos').removeClass('active');
		$('a#switch-to-polaroids').addClass('active');
		return false;
	});
}

// get model items via ajax
function getModelsByLetter(url, alink) {
	// remove highlights
	$('a.letterselect').removeClass('active');
	// add new highlight
	$(alink).addClass('active');
	// show indicator
	$('div#model-list-selecter').css('background', 'url(/images/roller.gif) center center no-repeat');
	// make ajax call
	$('div#model-list-selecter').empty();
	$('div#model-list-selecter').load(url, {ajax: 'true'}, function() {
		rebindModelDetailLinks();	
		// hide indicator
		$('div#model-list-selecter').css('background', 'none');
	});
}
						   
// handle model detail cart adder link for models in minibook
function clearAllCartAdderLinks(cartResource) {
	var l = 'li.navi-' + cartResource + '.cart-adder a';
	$(l).removeClass('disabled');
	$(l).addClass('enabled');
}
function disableCartAdderLink(id, cartResource) {
	var l = 'li.navi-' + cartResource + '.cart-adder a[@rel=' + id + ']';
	$(l).addClass('disabled');
	$(l).removeClass('enabled');
}
						   
// change header cart state
function changeCartState(number, resource) {
	$(resource).text(number);
}