$(document).ready(function(){
    searchfield();
    zipsearch();
    history();
    zoomicons();
    startpagequips();
    accordion();
    
    // Mouse-Over Marker für die Subnavigation
    $('#subnavi li a').hover(function(){
        $(this).parent().addClass('hover');
    }, function(){
        $(this).parent().removeClass('hover');
    });
    
    // Productflow
    $('.user-marx-pishop #header .left').click(function(){
        productflow(-1);
        return false;
    });
    $('.user-marx-pishop #header .right').click(function(){
        productflow(1);
        return false;
    });
    
});

function urlparam(name) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) return "";
    else return results[1];
}

function accordion() {
    var open = urlparam('tab');
    var allopen = (window.location.href.indexOf('sword_list') != -1);
    if (open) { open = parseInt(open); }
    else {open = false; }
    if (allopen) {
        $(".accordion").each(function(){
            var a = $(this);
            a.find('div.csc-header').click(function(){
                a.find('div.csc-header').unbind('click');
                a.accordion({
                    autoHeight: false,
                    collapsible: true,
                    active: false,
                    header: 'div.csc-header'
                });
            });
        });
    }
    else {
        $( ".accordion" ).accordion({
            autoHeight: false,
            collapsible: true,
            active: open,
            header: 'div.csc-header'
        });
    }
}

function searchfield() {
    var s = $('#searchtext');
    s.css('color', '#888888').val('Suche');
    s.focus(function(){
        if ($(this).val() == 'Suche') {
            $(this).css('color', 'black').val('')
        }
    });
    s.blur(function(){
        if ($(this).val() == '') {
            $(this).css('color', '#888888').val('Suche');
        }
    });
}

function zipsearch() {
    s = $('.zip');
    s.css('color', '#888888').val('Ihre PLZ');
    s.focus(function(){
        if ($(this).val() == 'Ihre PLZ') {
            $(this).css('color', 'black').val('')
        }
    });
    s.blur(function(){
        if ($(this).val() == '') {
            $(this).css('color', '#888888').val('Ihre PLZ');
        }
    });
}

function history() {
    $('ul.history li a').hover(function(){
        $(this).stop().animate({
            height:'160px',
            backgroundColor: '#1d3552'
        }, 300);
    }, function(){
        $(this).stop().animate({
            height:'80px',
            backgroundColor:'#bbbbbb'
        }, 300);
    });
    $('ul.history li.active a').unbind();
}

function zoomicons() {
     $('.zoomicons a > img').each(function(){
        $(this).css('verticalAlign', 'top');
        var p = $(this).parent();
        p.css('verticalAlign', 'top');
        var icon = $('<img src="fileadmin/templates/img/zoom.gif" />');
        icon.css('verticalAlign', 'top');
        icon.appendTo(p);
        p.fancybox();
    });
}

function startpagequips() {
    $('#people li').each(function(){
        if ($(this).hasClass('active')) { return; }
        $(this).hover(function(){
            $(this).animate({'opacity':1.0}, 'fast');
        }, function(){
            $(this).animate({'opacity':0.5}, 'fast');
        });
    });
}

var productpos = 2;
var shifttable = [
    {left:'-50px', top:'30px', zIndex:100, opacity:0.0},
    {left:'50px',  top:'50px', zIndex:300, opacity:1.0},
    {left:'150px', top:'70px', zIndex:500, opacity:1.0},
    {left:'250px', top:'90px', zIndex:700, opacity:1.0},
    {left:'350px', top:'70px', zIndex:600, opacity:1.0},
    {left:'450px', top:'50px', zIndex:400, opacity:1.0},
    {left:'550px', top:'30px', zIndex:200, opacity:0.0}
];

var products = [
    {name:'Product 1', price:'101,00 &euro;'},
    {name:'Product 2', price:'102,00 &euro;'},
    {name:'Product 3', price:'103,00 &euro;'},
    {name:'Product 4', price:'104,00 &euro;'},
    {name:'Product 5', price:'105,00 &euro;'},
    {name:'Product 6', price:'106,00 &euro;'},
    {name:'Product 7', price:'107,00 &euro;'},
    {name:'Product 8', price:'108,00 &euro;'},
    {name:'Product 9', price:'109,00 &euro;'},
    {name:'Product 10', price:'110,00 &euro;'},
    {name:'Product 11', price:'111,00 &euro;'}
];

function productflow(dir) {
    productpos -= dir;
    if (productpos < 0) { productpos += products.length; }
    if (productpos >= products.length) { productpos -= products.length; }
    var maxpos = shifttable.length;
    $('.user-marx-pishop .offer').each(function(){
        var pos = parseInt($(this).attr('rel'));
        var newpos = pos + dir;
        if (newpos < 0 || newpos >= maxpos) { return; }
        $(this).attr('rel', newpos).css('zIndex', shifttable[newpos].zIndex);
        $(this).animate(shifttable[newpos], 'fast', function(){
            if (newpos == 0 || newpos == maxpos-1) { $(this).remove(); }
        });
    });

    if (dir > 0) {
        var newindex = productpos - 2;
        if (newindex < 0) { newindex += products.length; }
        var newe = get_product(newindex);
        newe.attr('rel', 1).css(shifttable[0]).css('zIndex', shifttable[1].zIndex);
        newe.appendTo($('.user-marx-pishop #header'));
        newe.animate(shifttable[1], 'fast');
    }
    if (dir < 0) {
        var newindex = productpos + 2;
        if (newindex >= products.length) { newindex -= products.length; }
        var newe = get_product(newindex);
        newe.attr('rel', maxpos-2).css(shifttable[maxpos-1]).css('zIndex', shifttable[maxpos-2].zIndex);
        newe.appendTo($('.user-marx-pishop #header'));
        newe.animate(shifttable[maxpos-2], 'fast');
    }
}

function get_product(index) {
    var p = $('<div class="offer pngfix" />').css('position', 'absolute');
    p.append('<h3>'+products[index]['name']+'</h3>');
    p.append('<div class="imagecontainer"></div>');
    p.append('<a class="buy" href="#">kaufen</a>');
    p.append('<span class="price">'+products[index]['price']+'</span>');
    return p;
}
