

var toggleHomeBoxes = function(id)
{
    if($('#homebox'+id, '#maincontent').hasClass('hb_open'))
    {
        $('.homebox', '#maincontent').removeClass('hb_closed').removeClass('hb_open').addClass('hb_normal');
        
        $('.hb_normal > .hb_first', '#maincontent').slideDown('slow');
        $('.hb_normal > .hb_more', '#maincontent').slideUp('slow');
        
        $('.toggleLink', '#maincontent').html('Mehr Produkte');
    }
    else
    {
        $('.homebox', '#maincontent').removeClass('hb_normal').removeClass('hb_open').addClass('hb_closed');
        $('#homebox'+id, '#maincontent').removeClass('hb_normal').removeClass('hb_closed').addClass('hb_open');
        
        $('.hb_open > .hb_first', '#maincontent').slideDown('slow');
        $('.hb_open > .hb_more', '#maincontent').slideDown('slow');
        
        $('.hb_closed > .hb_first', '#maincontent').slideUp('slow');
        $('.hb_closed > .hb_more', '#maincontent').slideUp('slow');
        
        $('.toggleLink', '#maincontent').html('Mehr Produkte');
        $('#toggleLink-' + id).html('Zur&uuml;ck');
    }
    
    
}

var initHomeBoxes = function()
{
    var homeBoxes = $('.homebox', '#maincontent');
    if(homeBoxes.length > 0)
    {
        homeBoxes.each(
            function(i)
            {
                if(i < homeBoxes.length)
                {
                    $(this).addClass('hb_normal');
                    $('#toggleLink-' + i)
                    .html('Mehr Produkte')
                    .data('i', (i))
                    .click(
                        function()
                        {
                            toggleHomeBoxes($(this).data('i'));
                            return false;
                        }
                    )
                }
            }
        );
    }
}

var trackEvent = function(category, action, label)
{
	if(pageTracker != undefined)
	{
		pageTracker._trackEvent(category, action, label);
	}
}

$(document).ready(function()
{
    $('#show-manufacturers-all')
        .fancybox({
            'imageScale': false,
            'hideOnContentClick': false,
            'zoomOpacity': true,
            'overlayShow': true,
            'overlayOpacity': 0.8,
            'frameWidth': 921,
            'frameHeight': 570,
            'zoomSpeedIn': 800,
            'zoomSpeedOut': 250
        })
    ;
    
	$('a.video', '#column_right')
		.fancybox({
            'imageScale': false,
			'hideOnContentClick': false,
			'zoomOpacity': true,
			'overlayShow': true,
            'overlayOpacity': 0.8,
            'frameWidth': 480,
			'frameHeight': 360,
			'zoomSpeedIn': 400,
			'zoomSpeedOut': 250
		})
	;
	
    $('#image')
        .fancybox({
            'hideOnContentClick': true,
            'imageScale': false,
            'centerOnScroll': false,
            'zoomOpacity': true,
            'overlayShow': true,
            'overlayOpacity': 0.9,
            'zoomSpeedIn': 400,
            'zoomSpeedOut': 250
        })
    ;
    
    initHomeBoxes();
    
    $("a#agb_link", "#checkout_payment")
        .fancybox({
            'hideOnContentClick': true,
            'zoomOpacity': true,
            'overlayShow': true,
            'overlayOpacity': 0.8,
            'frameWidth': 600,
            'frameHeight': 500,
            'zoomSpeedIn': 400,
            'zoomSpeedOut': 250
        })
    ;
    
    $('.submit_contact', '#priceTable').click(
        function()
        {  
            var scope = $(this).parent();
            
            var phone = $(".contact_phone", scope).val();
            var message = $(".contact_message", scope).val();
            var product = $(".contact_product", scope).val();
            var form = $(".contact_form", scope).val();
            
            var email = $(".contact_email", scope).val();
            if(!isEmail(email) || email == '')
            {
                $('#' + form + ' span').html('Bitte geben Sie eine g&uuml;ltige E-Mail-Adresse an.');
                return false;
            }
            
            email = email.replace(/&/g,"**am**");
            email = email.replace(/=/g,"**eq**");
            email = email.replace(/\+/g,"**pl**");
            
            if(phone != undefined && phone != '') {
                phone = phone.replace(/&/g,"**am**");
                phone = phone.replace(/=/g,"**eq**");
                phone = phone.replace(/\+/g,"**pl**");
            } else {
                phone = "";
            }
            
            if(message != undefined && message != '') {
                message = message.replace(/&/g,"**and**");
                message = message.replace(/=/g,"**equal**");
                message = message.replace(/\+/g,"**plus**");
            } else {
                message = "";
            }
            
            $.post(
                "http://www.neue-musik-laden.de/media/content/ajaxcontact.php",
                {
                    form: form,
                    email: email,
                    phone: phone,
                    message: message,
                    product: product
                },
                function(data)
                {
                    if(data.error != undefined) {
                        $('#' + data.form + ' span').html(data.error);
                    } else {
                        $('#' + data.form + ' div').html(data.success).css('color', 'green');
                        $('#' + data.form + ' input[type=submit]').hide();
                    }
                },
                "json"
            );
            
            return false;
        }
    );
	
	$('.voting', '#comment-form').each(
		function()
		{
			var vv = parseFloat($(this).attr('title'));
			for(var i = 1; i <= 5; i++)
			{
				var star = $('.star_' + i + ' span', this);
				if(i <= Math.floor(vv))
				{
					star.css('width', '20px');
				}
				else
				{
					if(i - 1 < vv)
					{
						star.css('width', (20 * (vv - Math.floor(vv))) + 'px');
					}
					else
					{
						star.css('width', '0');
					}
				}
			}
		}
	);
	
	initVoting();
	
	$('#comment-form').submit(
		function()
		{
			var selectedVoteValue = $('#vote_value').val();
			var commentName = $('#comment_name').val();
			var commentValue = $('#comment_text').val();
			
			
            $.post(
                "http://www.neue-musik-laden.de/media/content/ajaxcomment.php",
                {
                    vote: selectedVoteValue,
                    name: commentName,
                    comment: commentValue
                },
                function(data)
                {
                    if(data.error != undefined) {
                        alert(data.error);
                    }
                    else if(data.reload != undefined) {
                        window.location.href = window.location.href;
                    } else {
                        alert(data.success);
                        $('#comment_name').val('');
                        $('#comment_text').val('');
                        $('#comment-submit').attr("disabled", "disabled");
                    }
                },
                "json"
            );
			
			return false;
		}
	);
	
	$('a:not(.submit)[href*="#"]').click(
		function()
		{
			if($(this).attr('href').length > 1 && location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname)
			{
				var $target = $(this.hash);
					$target = $target.length && $target || $('[@name=' + this.hash.slice(1) +']');
					if ($target.length)
					{
						var targetOffset = $target.offset().top;
						$('html,body').animate({scrollTop: targetOffset}, 1000);

						return false;
					}
			}
		}
	);
});


var initVoting = function()
{
	initVotingDisplay();
	
	$('#voting').mouseout(
		function(event)
		{
			initVotingDisplay();
		}
	);
	
	$('.star', '#voting').mouseover(
		function(event)
		{
			var nodeId = $(this).prevAll().length;
			for(var i = 1; i <= 5; i++)
			{
				if(nodeId >= i - 1)
				{
					$('span', '#star_' + i).css('width', '20px');
				}
				else
				{
					$('span', '#star_' + i).css('width', '0px');
				}
			}
		}
	);
	
	$('.star', '#voting').click(
		function(event)
		{
			$('#vote_value').val($(this).prevAll().length + 1);
			initVotingDisplay();
		}
	);
}

var initVotingDisplay = function()
{
	var selectedVoteValue = $('#vote_value').val();
	var currentVoteValue = selectedVoteValue == '' ? parseFloat($('#vote_current_value').val()) : parseFloat(selectedVoteValue);
	
	for(var i = 1; i <= 5; i++)
	{
		if(i <= Math.floor(currentVoteValue))
		{
			$('span', '#star_' + i).css('width', '20px');
		}
		else
		{
			if(i - 1 < currentVoteValue)
			{
				$('span', '#star_' + i).css('width', (20 * (currentVoteValue - Math.floor(currentVoteValue))) + 'px');
			}
			else
			{
				$('span', '#star_' + i).css('width', '0');
			}
		}
	}
}


var selected;
var submitter = null;
function submitFunction() { submitter = 1; }
function popupWindow(url) { window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150') }
function selectRowEffect(object, buttonSelect) {
    if(!selected) {
        if(document.getElementById) {
            selected = document.getElementById('defaultSelected');
        } else {
            selected = document.all['defaultSelected'];
        }
    }
    if(selected) selected.className = 'moduleRow';
    object.className = 'moduleRowSelected';
    selected = object;
    
    if(document.getElementById('payment'[0])) document.getElementById('payment'[buttonSelect]).checked=true;
}
function rowOverEffect(object) { if(object.className == 'moduleRow') object.className = 'moduleRowOver'; }
function rowOutEffect(object) { if(object.className == 'moduleRowOver') object.className = 'moduleRow'; }
function popupImageWindow(url) { window.open(url,'popupImageWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150') }

function getUrlParams() {
    var hier = document.URL;
    var parameterzeile = hier.substr((hier.indexOf("?")+1));
    var trennpos;
    var endpos;
    var paramname;
    var paramwert;
    var urlParams = new Array();
    while (parameterzeile != "") {
        trennpos = parameterzeile.indexOf("=");
        endpos = parameterzeile.indexOf("&");
        if (endpos < 0) endpos = 500000;
        paramname = parameterzeile.substr(0,trennpos);
        paramwert = parameterzeile.substring(trennpos+1,endpos);
        urlParams[paramname] = paramwert;
        parameterzeile = parameterzeile.substr(endpos+1);
    }
    return urlParams;
}

function openPopup(url, name, w, h, features) {
    popUp = window.open(url, name, 'width='+ w +',height='+ h +','+ features);
    if(popUp.moveTo) popUp.moveTo((screen.width - w) * .5, (screen.height - h) * .5);
    if(popUp.focus) popUp.focus();
}