$(document).ready(function() {
	
	// 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();
	
	// 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(48.155098, 11.575555), 15);
		var point = new GLatLng(48.155098, 11.575555);
		var marker = createMarker(point);
  		map.addOverlay(marker);
  		GEvent.trigger(marker, 'click');
	}
	
	$('.detail-view-toggler a').click(function() {
		var handle = $(this);
		if(!$(handle).parent().is('.disabled')) {
			if(!$('#model-detail-holder').is(':visible')) {
				// show detail view
				$('#model-detail-holder').slideDown('slow', function() {
		    		// show indicator
					$(handle).text('hide detail view').attr('title', 'hide detail view').parent().addClass('hide-detail-view').removeClass('show-detail-view');
			    });
			} else {
				// hide detail view
				$('#model-detail-holder').slideUp('slow', function() {
			    	$(handle).text('show detail view').attr('title', 'show detail view').parent().addClass('show-detail-view').removeClass('hide-detail-view');
			    });
			}
		}
		return false;
	});

	$('.head-sheet-toggler a').click(function() {
		var handle = $(this);
		if(!$('#model-result-wrapper').is(':visible')) {
			// show result wrapper
			$('#model-result-wrapper').slideDown('slow', function() {
				$(handle).text('hide head sheet').attr('title', 'hide head sheet').parent().addClass('hide-head-sheet').removeClass('show-head-sheet');
		    });
		} else {
			// hide result wrapper
			$('#model-result-wrapper').slideUp('slow', function() {
				$(handle).text('show head sheet').attr('title', 'show head sheet').parent().addClass('show-head-sheet').removeClass('hide-head-sheet');
		    });
		}
		return false;
	});
	
});

// create gmaps marker
function createMarker(point) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml('<img src="/images/logo_small.png" /><div style="color: #000; font-size: 11px; margin-top: 8px;">georgenstraße 39<br />80799 munich<br /><br />phone: +49 (0) 89 / 330 087 0</div>');
	});
	return marker;
}

// bind model thumb to detail view by click event
function rebindModelDetailLinks() {
	$('.model-item a').click(function() {
		// add class to current clicked item
		$('.model-item').removeClass('clicked');
		$(this).parent().parent().addClass('clicked');
		// show detail holder
		var dh = $('#model-detail-holder');
		var dhi = $('#model-detail-holder-inner', dh);
		var link = $(this);
		var location = $(link).attr('href');
		var loadContent = function(url, container, callback) {
			$(container).load(url, {ajax: 'true'}, function() {
				// handle add-to-cart links
				$('li.cart-adder a').click(function() {
					$(this).removeClass('enabled');
					$(this).addClass('disabled');
				});
				// scroll to detail container 
				$.scrollTo('#content', 1000);
				// init thickbox links
				callback();
			});
		}
		if($(dh).is('.empty')) {
			// slide down the container
			$(dh).hide().css('height', '505px');
			$(dh).slideDown('fast', function() {
	    		// show indicator
	    		$(dh).css('background', 'url(/images/roller.gif) center center no-repeat');
	    		// insert new content
	    		$(dhi).empty();
	    		loadContent(location, dhi, function() {
	    			// image viewing options
	    			rebindImageViewOptions();
					// hide indicator
					$(dh).css('background', 'none');
	    			$(dh).removeClass('empty');
					tb_init($('a.thickbox', dh));
					// handle the detail toggler
					$('.detail-view-toggler').removeClass('disabled');
					$('.detail-view-toggler a').text('hide detail view').attr('title', 'hide detail view').parent().addClass('hide-detail-view').removeClass('show-detail-view');
	    		});
		    });
		} else {
			if($(dh).is(':visible')) {
				$(dhi).fadeOut('fast', function() {
		    		// show indicator
		    		$(dh).css('background', 'url(/images/roller.gif) center center no-repeat');
		    		$(dhi).empty();
		    		// insert new content
		    		loadContent(location, dhi, function() {
		    			// image viewing options
		    			rebindImageViewOptions();
		    			$(dhi).fadeIn('fast', function() {
							tb_init($('a.thickbox', dh));
							// handle the detail toggler
							$('.detail-view-toggler a').text('hide detail view').attr('title', 'hide detail view').parent().addClass('hide-detail-view').removeClass('show-detail-view');
		    			});
		    		});
				});
			} else {
				$(dhi).empty();
				$(dh).hide().css('height', '505px');
				$(dh).slideDown('fast', function() {
		    		// show indicator
		    		$(dh).css('background', 'url(/images/roller.gif) center center no-repeat');
		    		// insert new content
		    		$(dhi).empty();
		    		loadContent(location, dhi, function() {
		    			// image viewing options
		    			rebindImageViewOptions();
						// hide indicator
						$(dh).css('background', 'none');
		    			$(dh).removeClass('empty');
						tb_init($('a.thickbox', dh));
						// handle the detail toggler
						$('.detail-view-toggler').removeClass('disabled');
						$('.detail-view-toggler a').text('hide detail view').attr('title', 'hide detail view').parent().addClass('hide-detail-view').removeClass('show-detail-view');
		    		});
			    });
			}
		}
		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
	$('#model-result-wrapper').css('background', 'url(/images/roller.gif) center center no-repeat');
	// make ajax call
	$('#model-result-wrapper').empty();
	$('#model-result-wrapper').load(url, {ajax: 'true'}, function() {
		rebindModelDetailLinks();	
		// hide indicator
		$('#model-result-wrapper').css('background', 'none');
	});
	return false;
}
						   
// 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);
}
