$(document).ready(function() {
    $('#navigation').slideSubMenu();

    replaceAttr();
    createAjaxLinks();
    var hash = window.location.href
    
    $.historyInit(loadPage, hash);

    setNaviagtionHeight();
    wishlistLink();
});

function wishlistLink() {
    $('.produktliste .produkt').each(function() {
		if ($(this).find('a.wishlistoverlay').length <= 0) {
			var href = $(this).find('a:first').attr('href')
			var href = href.replace('details', 'wishlist');
			var href = href.replace('shop', 'shop/ajax');
			$(this).find('a:first').before('<a class="wishlistoverlay" href="' + href + '">merken</a>')
			$(this).find('.wishlistoverlay').bind('click', function() {
				$.get(this, function(){
					$(this.url).after('<span class="wishlistnotify">gemerkt</span>');
					$(this.url).next('span').slideDown().delay('1200').slideUp('normal', function(){
					$(this).remove()
					});
				})
				return false;
			}).hide();

		}
    })

    $('.produkt').hover(
	function() {
	    $(this).find('.wishlistoverlay').slideDown();
	},
	function() {
	    $(this).find('.wishlistoverlay').slideUp();
	}
	);

}

function replaceAttr() {
    var attrOrg = $('.com-single').find('.texte').find('.attribute');
    if ($(attrOrg).find('form').length >= 1) {
	$('.com-single').find('.attribute:hidden').show().find('div').html($(attrOrg).html());
    }

    $(attrOrg).remove()

}

function createAjaxLinks() {
    ajaxLinks = $('.ajax_link');
    if (ajaxLinks.length >= 1) {
	$(ajaxLinks).live('click', function() {
	    $('div.com-single:first')
	    .html('<img src="fileadmin/templates/img/ajax-loader.gif" />')
	    .css('text-align', 'center')
	    loadPage(this, true);
	    return false;
	})
    }

    return true;
}

function loadPage(url, from_link) {

    if(from_link) {
		var hash = url.href;
	
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
		return true;
    }

    if (url == '') {
	url = window.location
    }
    var html = $.ajax({
	url: url,
	async: false
    }).responseText;

    $('.com-single')
    .html(($(html).find('.com-single')))
    .css('text-align', 'left')
    setTitle();
    replaceAttr();
}
function setTitle() {
    var titleArr = document.title.split("|");
    document.title = $('.com-single').find('h2 > a').text() + ' | ' + titleArr[1]
}


function debug(data) {
    if ($.browser.mozilla) {
	console.log(data);
    }
}

function setNaviagtionHeight() {
    $('#navigation').height(
	$(document).height() - $('#kopf').height()
	)
}

