﻿jQuery(document).ready(function() {
    
    // accordion
    var initAccordion = function() {
        jQuery('.accordion').each(function() {
            var acc = jQuery(this);
            jQuery('dt', acc).addClass('hidden');
               jQuery('dd', acc).hide();  
               jQuery('dd:lt(1)', acc).show();   
               jQuery('dd:lt(2)', acc).show();   
               jQuery('dd:lt(3)', acc).show(); 
               // jQuery('dd:lt(4)', acc).show();   
               // jQuery('dd:lt(5)', acc).show();  
               jQuery('dd:last', acc).addClass('last');

            // handle mouseover
            jQuery('dt', acc).bind('mouseover', function() {
                jQuery(this).addClass('hover');
            });
            jQuery('dt', acc).bind('mouseout', function() {
                jQuery(this).removeClass('hover');
            });
            // handle click
            jQuery('dt span', acc).click(function() {
                var cont = jQuery(this).parent().next('dd');
                var f = function(obj) {
                    if(jQuery(obj).parent().find(':hidden').length > 0) {
                        jQuery(obj).slideDown('fast', function() {
                            jQuery(this).prev().addClass('visible').removeClass('hidden');
                            if (typeof _oCM == "object" && typeof cmCreatePageElementTag == "function")
                                cmCreatePageElementTag("NEWS: " +(obj.prev().text().length>=48?obj.prev().text().substr(0,48)+ "...":obj.prev().text()), "Camera Home News");
                        });
                    } else {
                        jQuery(obj).slideUp('fast', function() {    
                            jQuery(this).prev().addClass('hidden').removeClass('visible');
                        });    
                    }
                }
                if(jQuery('dd:visible', acc).length > 0) {
                    var t = jQuery('dd:visible', acc);
                        jQuery(t).slideUp('fast', function() {
                            if(jQuery(t)[0] != jQuery(cont)[0]) {
                                f(cont);
                            }
                            jQuery(this).prev().addClass('hidden').removeClass('visible');
                        });
                } else {
                    f(cont);
                }
            });
        });
        setTimeout(function() {
                jQuery('#further-news-list .accordion.accordion-auto').each(function() {                     
             
                  //jQuery('dt:first span', this).click();               
                  //jQuery('dt:last span', this).click(); 

                 // open first item    
                });
        }, 1000);
    }
    initAccordion();
    
    // do the tabbing
    var initTabber = function() {
        jQuery('.tabber').each(function() {
            // init
            var tabContainer = jQuery('.tab-wrapper', this);
            var tabNavigator = jQuery('.tab-navigator', this);
            var showTab = function(tab_id) {
                jQuery('.tab#'+tab_id, tabContainer).fadeIn('slow');
            };
            var hideTab = function(tab_id) {
                jQuery('.tab#'+tab_id, tabContainer).fadeOut('slow');
            };
            // hide tabs
            jQuery('.tab', tabContainer).each(function() {
                jQuery(this).hide();
            });
            // show first tab
            var firstTab = jQuery('.tab:first', tabContainer);
            showTab(jQuery(firstTab).attr('id'));
            jQuery('li a', tabNavigator).click(function() {
                // handle navigator
                var item = jQuery(this);
                jQuery('.tab:visible', tabContainer).fadeOut('slow', function() {
                    showTab(jQuery(item).attr('href').replace('#', ''));
                });
                jQuery('li', tabNavigator).removeClass('active'); // remove active class
                jQuery(item).parent('li').addClass('active'); // add new active class
                return false;
            });
        });
    }
    initTabber();
    
});
