Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5), 10) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5), 10) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

var reviewCount = 0;

function cl(str){
    if(window.console) {
        if(Prototype.Browser.Gecko) {
            return console.log(str);
        }
    } else {
        return false;
    }
}
var testEnumerator = 0;
function cltest(str){
    testEnumerator++;
    var testStr = (!str) ? 'test: ' + testEnumerator : str;

    if(Prototype.Browser.Gecko) {
        cl(testStr);
    } else {
        alert(testStr);
    }
}


var currentPos = 0;
var movePx = 226;
var timelineWidth;
var totalTimelineWidth;
var timelineClickMove = 10;
var percentage = 0;
var wrapperMarginLeft = 0;

function slideTimeline(direction) {
    percentage += ((percentage <= 0 && direction < 0) || (percentage >= 100 && direction > 0)) ? 0 : direction*timelineClickMove;
    $$('.ui-slider-handle')[0].morph('left:' + percentage+'%');
    
    timelineWidth = $$('.timeline-events-wrapper')[0].getWidth();
    totalTimelineWidth = $$('.timeline-events')[0].getWidth();
    wrapperMarginLeft = (direction == 1) ? -1*direction*Math.round((timelineWidth - totalTimelineWidth)*(percentage*.01)) : -1*direction*Math.round((timelineWidth - totalTimelineWidth)*(-percentage*.01));
    $$('.timeline-events-wrapper')[0].morph('margin-left:' + wrapperMarginLeft+'px');

/*
    var element;
    if(element = $$('.timeline-events-wrapper')[0]) {
        var theMove = direction*movePx;
        currentPos += -1 * theMove;
        if((currentPos < timelineWidth && direction < 0) || (currentPos >= 0 && direction > 0)) {
            new Effect.Move(element, {x:theMove, mode: 'relative', duration:.3, transition: Effect.Transitions.sinoidal});
        } else {
            currentPos += theMove;
        }
    }
*/
}

document.observe('dom:loaded', function() {

    if($$('.category-banner').length > 0){
        var bannerHeight = parseFloat($$('.category-banner').invoke('getStyle', 'height'));
        $$('.product-fade-left').invoke('setStyle', {top: -bannerHeight + 10 + 'px'});
        $$('.product-fade-right').invoke('setStyle', {top: -bannerHeight + 'px'});
    }

    if($('content_concert')){
        $$('.post > h2').invoke('setStyle', 'display: none');
    }

    if(($$('.category-music').length > 0) || ($('content_blog'))){
        $$('.breadcrumbs').invoke('setStyle', {visibility:'visible'});
    }

    if($$('.bios-wrapper').length > 0){
        var lis = $$('.bios-wrapper ul li').length;
        var lastCount = lis % 5;
        var liKey = lis - lastCount;
        var liWidth = $$('.bios-wrapper ul li')[liKey].getWidth();
        var liRemainder = 5 - lastCount;
        var marginLeft = (liRemainder * liWidth)/2;
        $$('.bios-wrapper ul li')[liKey].setStyle({marginLeft: marginLeft + 'px'});
    }
    
    $$('a[rel="external"]').each(function(link) {
        if(link.readAttribute('href') != '' && link.readAttribute('href') != '#') {
            link.writeAttribute('target', '_blank');
        }
    });

    var labelWidth = 2;
    if($$('.cf-ol .emailreqtxt').length > 0){
        $$('.cf-ol .emailreqtxt').each(function(el){
            var textWidth = el.up().select('label span')[0].getWidth();
            el.setStyle({left:(textWidth + labelWidth) + 'px'});
            el.update('*');
        });
    }

    if($$('.cf-ol .reqtxt').length > 0){
        $$('.cf-ol .reqtxt').each(function(el){
            var textWidth = el.up().select('label span')[0].getWidth();
            el.setStyle({left:(textWidth + labelWidth) + 'px'});
            el.update('*');
        });
    }

    if($$('.category-press-releases').length > 0){
        $$('.col-main').invoke('writeAttribute', 'id','content_press_room');
    }

    var imgFound = 0;
    $$('#content_blog .post').each(function(el){
        var el = $(el);
        el.getElementsBySelector('img').each(function(ele){
            if(!imgFound){
               imgFound = 1;   
            } else {
                var ele = $(ele);
                ele.hide();
            }
        });
        imgFound = 0;
    });


if(jQuery('.content-image').length){

    $$('div#content_bottom_main div a').invoke('observe', 'mouseover', function(){
        this.up().childElements().invoke('addClassName', 'active');
    });

    $$('div#content_bottom_main div a').invoke('observe', 'mouseout', function(){
        this.up().childElements().invoke('removeClassName', 'active');
    });

}

$$('#nav > li > a').invoke('addClassName', 'level0');

var currentPos = 0;
var movePx = 226;
var timelineWidth = 0;
var endYearSpaceDividers = 156;

    if($$('.timeline-events-wrapper')[0]) {
        var numEvents = $$('.event').length;
        timelineWidth = (numEvents-4) * movePx;
        $$('.timeline-events-wrapper')[0].setStyle({width:(numEvents*movePx+endYearSpaceDividers) + 'px'});
    }

    if(jQuery('.timeline-events').length){

        jQuery('div.event')
        .mouseenter(function(){
            jQuery(this).find('.event-details').fadeIn();
            jQuery('.tooltip-declaration').fadeOut();
        })
        .mouseleave(function(){
            jQuery(this).find('.event-details').fadeOut();
        });

    }

        // if(jQuery('.timeline-events').length){
        //  $('.timeline-events').cycle({
        //      fx:     'scrollHorz',
        //      prev:   '#previous',
        //      next:   '#next',
        //      timeout: 0
        //  });
        // }

        if(jQuery('.timeline-events').length){
            jQuery(function() {
                //scrollpane parts
                var scrollPane = jQuery('.col-main');
                var scrollContent = jQuery('.timeline-events-wrapper');

                //build slider
                var scrollbar = jQuery(".scroll-bar").slider({
                    slide:function(e, ui){
                        if( scrollContent.width() > scrollPane.width() ){ scrollContent.css('margin-left', Math.round( ui.value / 100 * ( scrollPane.width() - scrollContent.width() )) + 'px'); }
                        else { scrollContent.css('margin-left', 0); }
                    }
                });

                //append icon to handle
                var handleHelper = scrollbar.find('.ui-slider-handle')
                .mousedown(function(){
                    scrollbar.width( handleHelper.width() );
                })
                .mouseup(function(){
                    scrollbar.width( '100%' );
                })
                .append('<span class="ui-icon ui-icon-grip-dotted-vertical"></span>')
                .wrap('<div class="ui-handle-helper-parent"></div>').parent();

                //change overflow to hidden now that slider handles the scrolling
                scrollPane.css('overflow','hidden');

                // size scrollbar and handle proportionally to scroll distance
                //                 function sizeScrollbar(){
                //                     var remainder = scrollContent.width() - scrollPane.width();
                //                     var proportion = remainder / scrollContent.width();
                //                     var handleSize = scrollPane.width() - (proportion * scrollPane.width());
                //                     scrollbar.find('.ui-slider-handle').css({
                //                         width: handleSize,
                //                         'margin-left': -handleSize/2
                //                     });
                //                     handleHelper.width('').width( scrollbar.width() - handleSize);
                //                 }

                //reset slider value based on scroll content position
                function resetValue(){
                    var remainder = scrollPane.width() - scrollContent.width();
                    var leftVal = scrollContent.css('margin-left') == 'auto' ? 0 : parseInt(scrollContent.css('margin-left'), 10);
                    var percentage = Math.round(leftVal / remainder * 100);
                    scrollbar.slider("value", percentage);
                }
                //if the slider is 100% and window gets larger, reveal content
                function reflowContent(){
                    var showing = scrollContent.width() + parseInt( scrollContent.css('margin-left'), 10 );
                    var gap = scrollPane.width() - showing;
                    if(gap > 0){
                        scrollContent.css('margin-left', parseInt( scrollContent.css('margin-left'), 10 ) + gap);
                    }
                }

                //change handle position on window resize
                jQuery(window)
                .resize(function(){
                    resetValue();
                    // sizeScrollbar();
                    reflowContent();
                });
                //init scrollbar size
                // setTimeout(sizeScrollbar,10);//safari wants a timeout
            });
        }

        if(jQuery('#slideshow ul').length){
            jQuery('#slideshow ul').cycle({
                speed: 1500,
                timeout: 8000,
                pager: '#slideshow_nav',
                pause: 1,
                pauseOnPagerHover: 1,
                prev:   '#slideshow-previous',
                next:   '#slideshow-next'
            });
        }

        if(jQuery('#product_listing_cycle').length){
            jQuery('#product_listing_cycle').cycle({
                fx:     'scrollHorz',
                prev:   '#prev1',
                next:   '#next1',
                timeout: 0,
                pager: '.pager',
                speed: 2500
            });
        }

        // var navWidth = $('nav li.level0').getWidth();
        // var navUlWidth = ($('nav li.level0').getWidth()+20);
        // var navUlLeft = (($('nav li.level0').getWidth()-navUlWidth)/2);
        // 
        // $('nav ul.level0').setStyle({
        //  left: navUlLeft + 'px',
        //  width: navUlWidth + 'px'
        // });

        // if(jQuery('.catalog-product-view').length > 0){
            // $("p.product-image img").fancybox();

        if($$('.product-tabs div')[0]){
            $$('.product-tabs div').invoke('observe', 'click', function(){
                this.siblings().invoke('removeClassName','active');
                this.addClassName('active');
            });
        }

        if($$('div.product-single-tracks')[0]){
            $$('div.product-single-tracks')[0].observe('click', function(){
                    jQuery('#individual_tracks_wrapper').show();
                    jQuery('#product_reviews').hide();
                    jQuery('.product-options-bottom').show();
                    jQuery('#product_reviews_write').hide();
                });
        }

        if($$('div.product-reviews')[0]){
            $$('div.product-reviews')[0].observe('click', function(){
                    jQuery('#product_reviews').show();
                    jQuery('#individual_tracks_wrapper').hide();
                    jQuery('.product-options-bottom').hide();
                    jQuery('#product_reviews_write').hide();
                });
        }

        if($$('div.product-write-reviews-first')[0]){
            $$('div.product-write-reviews-first')[0].observe('click', function(){
                jQuery('#product_reviews_write').show();
                jQuery('.product-options-bottom').hide();
                jQuery('#product_reviews').hide();
                jQuery('#individual_tracks_wrapper').hide();
            });
        }

        if($$('div.product-write-reviews')[0]){
            $$('div.product-write-reviews')[0].observe('click', function(){
                jQuery('#product_reviews_write').show();
                jQuery('.product-options-bottom').hide();
                jQuery('#product_reviews').hide();
                jQuery('#individual_tracks_wrapper').hide();
            });
        }
        if(Prototype.Browser.IE) {
            if($('product_listing_cycle')){
                $$('#product_listing_cycle .slide').invoke('setStyle', 'background: none');
            }
            if(Prototype.Browser.IE6) {
                var iePar = new Element('p'/*, {"class":"ie6-flash"}*/);
                // iePar.update('Please <a href="http://www.microsoft.com/windows/internet-explorer/worldwide-sites.aspx" rel="external">upgrade your broswer</a> to view the interactive calendar below.');
                $$('object').invoke('up').invoke('insert', {before:iePar});
                
            }
            
            if($$('#nav > li')[2] /*&& $$('#nav > li')[2].getChildElements().length > 0*/) {
                $$('#nav > li')[2].writeAttribute('onmouseover', '').writeAttribute('onmouseout', '');
            }
        }

        if(Prototype.Browser.WebKit){
            $$('#newsletter-validate-detail .button span').invoke('setStyle', {paddingLeft:'20px', paddingRight:'20px'});
            $$('.buttons-set button.button').invoke('setStyle', {marginLeft:'-5px'});
            $$('#slideshow ul li span').invoke('setStyle', {width:'400px'});
        }

        $$('#sidebar_wp a', '#navigation .categories a').each(function(el){
            var el = $(el);
            el.update('<span>' + el.innerHTML + '</span>');
        });
        
        $$('.bios-wrapper ul li a br').invoke('remove');

        //--fancybox fix for IE
        if(jQuery.fn.fancybox) { 
               var fbonstart = (jQuery('.fancybox-ie').length != 0) ? function(){if(jQuery('#fbiecssfix').length == 0) jQuery('head').append('<link id="fbiecssfix" rel="stylesheet" type="text/css" media="all" href="'+BASE_URL+'skin/cls/chanticl/css/jquery.fancybox-ie.css" />');} : null;
        
            //bios page fancybox
            jQuery('a[rel="bios_gallery"]').fancybox({
                'onStart': fbonstart,
                'overlayColor'  : '#000',
                'overlayOpacity': .7,
                'titleShow'     : true,
                'titleFormat' : function (title, currentArray, currentIndex, currentOpts) {
                    return '<span id="fancy-title-over">' + (currentIndex + 1) + '/' + currentArray.length + '</span>';
                }
            });
            /* Using custom settings */

            jQuery("p.product-image a").fancybox({
                'onStart': fbonstart,
                'titleShow': false
            });

            jQuery("a[rel=example_group]").fancybox({
                'onStart': fbonstart,
                'transitionIn' : 'none',
                'transitionOut' : 'none',
                'titlePosition' : 'over',
                'centerOnScroll' : true,
                'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
                    return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
                }
            });
        }

        if(Prototype.Browser.IE6) {
            $$("a[rel=example_group]").each(function(link){
                if(link.readAttribute('href') != '' && link.readAttribute('href') != '#') {
                    link.writeAttribute('target', '_blank');
                }
            });
        }

        
        //-- checkout page
        if($('donation')){
            $('donation').focus();
        }
        
        
            // helpTabs = new Control.Tabs('help_tabs',{  
                //     hover: false
            // });

            // new Control.Tabs('tracks_reviews_tabs');
            // }

            // if(Prototype.Browser.IE && parseFloat(navigator.appVersion.split("MSIE")[1]) == '6')
            // $$('body')[0].insert({
            //     top : '<div id="ie6msg"><h4>Did you know that your browser is out of date?</h4> <p>To get the best possible experience using our website we recommend that you upgrade your browser to a newer version. The current version is <a class="getie7" href="http://www.microsoft.com/windows/downloads/ie/getitnow.mspx">Internet Explorer 7</a> and <a class="getie7" href="http://www.microsoft.com/windows/Internet-explorer/beta/default.aspx">Internet Explorer 8</a> is available as a beta. The upgrade is free. If you’re using a PC at work you should contact your IT-administrator.</p>            <p>If you want to you may also try some other popular Internet browsers like <a class="ie6expl" href="http://www.opera.com">Opera</a>, <a class="ie6expl" href="http://mozilla.com">FireFox</a> or <a class="ie6expl" href="http://www.apple.com/safari/download/">Safari</a></p>           </div>'
            // });
            
            
});


