/**
 * @filename    ms.js
 * framework    jquery 1.4.2
 *
 * @author      Marko Stutz
 * @date        06.07.2010
 * @version     2.0.1
 *
**/

$(document).ready(function() {

    $('ul.nav-menu').msMenu();
    
    
    $('.js-hoverbox').each( function(index, element){
        var className = $(element).attr('hoverclass') || 'hover';
        $(element).hover( function(){ $(this).addClass(className); }, function(){ $(this).removeClass(className); } );
    });
    
    if($.browser.msie && ( $.browser.version == '6.0' || $.browser.version == '7.0' )){
        $('table.table-content-layout').attr('cellspacing', 10);
    }
    
    if($.browser.msie && ( $.browser.version == '6.0' )){
        $('.js-png-fix').pngFix({
            blankgif: ms.base.url+'out/basic/img/leer.gif'
        });
        $('.content-box h1:first-child, .content-box h2:first-child,.content-box h3:first-child,.content-box p:first-child').css('margin-top', '0');
    }
    
    // payment
    if( ms.isset('blIsPaymentForm') && blIsPaymentForm === true ){
        ms.forms.initPaymentForms();
        $('#id-order-button-group-bottom').show(1000);
    }
    
    // details
    if( ms.base.plugins.blPrettyPhoto === true ){
        $('.js-lightbox').prettyPhoto({
            theme: 'light_rounded',
            markup: '<div class="pp_pic_holder"><div class="pp_top"><div class="pp_left"></div><div class="pp_middle"></div><div class="pp_right"></div></div><div class="pp_content_container"><div class="pp_left"><div class="pp_right"><div class="pp_content"><div class="pp_loaderIcon"></div><div class="pp_fade"><a href="#" class="pp_expand" title="Bild erweitern">erweitern</a><div class="pp_hoverContainer"><a class="pp_next" href="#">weiter</a><a class="pp_previous" href="#">zur&uuml;ck</a></div><div id="pp_full_res"></div><div class="pp_details clearfix"><a class="pp_close" href="#">schliessen</a><p class="pp_description"></p><div class="pp_nav"><a href="#" class="pp_arrow_previous">zur&uuml;ck</a><p class="currentTextHolder">0/0</p><a href="#" class="pp_arrow_next">weitern</a></div></div></div></div></div></div></div><div class="pp_bottom"><div class="pp_left"></div><div class="pp_middle"></div><div class="pp_right"></div></div></div><div class="pp_overlay"></div><div class="ppt"></div>'
        });
    }
    if( ms.base.plugins.blMSIconGallery === true ){
        var productIconGallery = $('#id-js-gallerybox').msIconGallery({pictureBox:'#id-picture-box', pictureBackground:'.picture-box-background'});
    }
    if( ms.base.plugins.blAccordion === true && ms.base.plugins.blUI === true ){
        
        $('#id-accordion-article-details').accordion({
            icons:false,
            animated:false,
            autoHeight:false
        });
        
        var $actTreeContainer = $('#id-accordion-category-tree .js-category-tree-exp');
        var isInit = false;
        
        $('#id-accordion-category-tree').accordion({
            icons:{'header': 'ui-icon-triangle-1-e', 'headerSelected': 'ui-icon-triangle-1-s'},
            animated:false,
            autoHeight:false,
            header: 'h3',
            navigation: true,
            navigationFilter: function(index, ui){
                var url = $(this).attr('href');
                $(this).click(function(){
                    if(url){
                        window.location.href = url;
                    }
                });
            },
            active: false,
            change: function(event, ui){
                if( isInit === false ){
                    var url = ui.newHeader.find('.js-accordion-category-button').attr('href');
                    if(url){
                        var c = ui.newContent;
                        c.append('<div class="ui-dynatree-container ui-dynatree-lastsib ui-dynatree-statusnode-wait"><span class="ui-dynatree-connector"></span><span class="ui-dynatree-icon"></span><span class="ui-dynatree-title">Loading...</span></div>');
                    }
                }else{
                    isInit = false;
                }
            }
        });
        
        if($.browser.msie && ( ($.browser.version == '6.0') || ($.browser.version == '7.0') )){
            $('#id-accordion-category-tree .ui-accordion-content').width( $('#id-accordion-category-tree h3:first').width() );
        }
        
        if( $actTreeContainer.length > 0 ){
            if( $actTreeContainer.eq(0).hasClass('js-category-tree-has') ){
                var catid = $actTreeContainer.eq(0).attr('init');
                $actTreeContainer.eq(0).dynatree({
        			title: '',
        			rootVisible: false,
        			icon: false,
        			fx: { height: 'toggle', duration: 100 },
                    initAjax: {
                        url: ms.base.url+'msindex.php',
                        data: { cl: 'tree', id: catid, path: ms.base.actPath, act: ms.base.actCat },
                        success: function(dtnode){
                            if($actTreeContainer.length > 0){
                                isInit = true;
                                var index = ($actTreeContainer.eq(0).attr('index') * 1) - 1;
                                $('#id-accordion-category-tree').accordion( 'activate' , index );
                            }
                        }
                    },
        			onActivate: function(dtnode) {
                        if( !dtnode.data.url){
                            dtnode.data.url = 'index.php?cl=alist&cnid=' + dtnode.data.key;
                        }
                        window.location.href = ms.base.url + dtnode.data.url;
        			},
        			onLazyRead: function(dtnode){
                        dtnode.appendAjax({
                            url: ms.base.url+'msindex.php',
        		            data: { cl: 'tree', id: dtnode.data.key },
        		            success: function(dtnode){
      		                    
                            },
                            error: function(dtnode, XMLHttpRequest, textStatus, errorThrown){
                            }
                        });
        			}
        			
        		});
            }else{
                var index = ($actTreeContainer.eq(0).attr('index') * 1) - 1;
                $('#id-accordion-category-tree h3:eq('+index+')').addClass('ui-state-active');
            }
		}
        
    }
    // toolbar form search
    $('#id-toolbar-form-search-param').msFormElementMsg({defaultText:'Suche', stopSubmit:true});
    
    // user form order
    $('#id-order-remark').msFormElementMsg({defaultText:'Hier können Sie uns noch etwas mitteilen.'});
    
    // tooltips
    $('.js-tooltip').wTooltip({className: 'tooltip-box', style: null});
    
    // start
    if( ms.base.plugins.blCycle === true ){
        $('.js-cycle-box').cycle({
            fx: 'scrollRight',
            timeout: 8000,
            after: function(currSlideElement, nextSlideElement, options, forwardFlag){
                var currid = $(currSlideElement).attr("idext");
                var nextid = $(nextSlideElement).attr("idext");
                $('#'+currid).hide();
                $('#'+nextid).show('normal');
                return;
            }
        });
        $('#id-list-show-box').hover(
            function(){$('.js-cycle-box').cycle('pause')},
            function(){$('.js-cycle-box').cycle('resume')}
        );
    }
    
    if( ms.base.plugins.blRssFeed === true ){
        $('#id-rssfeed-box').rssfeed('http://www.e-special-shop.de/blog/feed', {limit: 5, header: false});
    }
    
    // calculate productlist 4cols, Class( row="js-calc-table-row", innerboxes="js-calc-box-1, js-calc-box-2, js-calc-box-3" )
    if( ms.isset('blCalcTableRows') && blCalcTableRows === true ){
        $('tr.js-calc-table-row').each(function(index){
            var aCalcBox, iMaxHeight;
            for(var i = 1; i <= 3; i++){
                aCalcBox = $(this).find('.js-calc-box-'+i);
                iMaxHeight = ms.calc.getHighestElement( aCalcBox );
                if( iMaxHeight > 0 ){
                    aCalcBox.each(function(){
                        $(this).height(iMaxHeight);
                    });
                }
            }
        });
    }
    
    ms.popup.box = $('#id-window-popup-box').msPopupWindow();
    
    $('#id-msg-tobasket').msPopupMsg();
    
});

