$(document).ready(function() {

    initMainMenu();

    // opens iframes
    $(".popup, .popup-link-with-arrow, .popup-link-without-arrow, #countdown-link").fancybox({
        'width'              : 920,
        'height'             : 504,
        'autoScale'          : false,
        'padding'            : 1,
        'scrolling'          : 'auto',
        'type'               : 'iframe',
        'centerOnScroll'     : true,
        'overlayShow'        : true,
        'overlayColor'       : '#000',
        'overlayOpacity'     : 0.8,
        'hideOnOverlayClick' : true,
        'onStart'            : disableResize,
        'onCancel'           : enableResize,
        'margin'             : 0
    });             
    
    initServiceStations();    

    initNewsRotation();         
    
    $('#service-station-submit').remove();                       
     
}); /////////////////////////////////////////////////////// end dom ready

var loading = false;
$(document).ajaxStart(function(){
   loading = true;
});
$(document).ajaxStop(function(){
   loading = false;
});
$(document).ajaxError(function(){
   loading = false;
});    

function setPosition(target, div, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, callback) { 
    var duration = 1500;
    switch(target) {
        case '#box1':   $(div).scrollTo(p1, duration, callback); break;
        case '#box2': $(div).scrollTo(p2, duration, callback); break;            
        case '#box3': $(div).scrollTo(p3, duration, callback); break;                    
        case '#box4': $(div).scrollTo(p4, duration, callback); break;                            
        case '#box5':   $(div).scrollTo(p5, duration, callback); break;
        case '#box6':   $(div).scrollTo(p6, duration, callback); break;
        case '#box7':   $(div).scrollTo(p7, duration, callback); break;
        case '#box8':   $(div).scrollTo(p8, duration, callback); break;
        case '#box9':   $(div).scrollTo(p9, duration, callback); break;                        
        case '#box10':   $(div).scrollTo(p10, duration, callback); break;                                
    }
};

function loadContent(url, target) { 
    $(target + ' .content').html('<img style="position:absolute; top: 50%; left: 50%;" src="/images/ajax-loader.gif" />');
    //fadein subnav 
    $('.subnav').fadeIn();                                  
    $(target + ' .content').load(url + " .inner", null, function(){
        initAfterLoad();                                                    
        initMainMenu();               
        switch(target) {
            case '#box1': initNewsRotation(); initMainMenu(); break;
            case '#box6': disableResize(); initCarousel(); initSubMenu(); break;                                                      
            case '#box9': initJParallax(); break;
            case '#box10': initArtGallery(); break;
        }    
    });
}  

// calls fancybox, used in collection details    
function  callFancy(my_href) {

    var j1 = document.getElementById("hiddenclicker");
    j1.href = my_href;
    jQuery('#hiddenclicker').trigger('click');
}

function initNewsRotation() {    
    $('#home-news-screen').serialScroll({
        target:'#sections',
        items:'td',
        axis:'x',
        event : 'mouseover',
        navigation:'#newspager a',
        duration:1000,
        force:true,    
        start: 0, 
        cycle:true,
        lock : false,
        interval:6000,

        onBefore:function( e, elem, $pane, $items, pos ){
            e.preventDefault();
            if( this.blur )
                this.blur();
             
            $('#newspager a').removeClass('activeSlide'); // all links
            $('#newspager a#newspager-link-' + pos).addClass('activeSlide'); // clicked link                                                               
                               
        },
        onAfter:function( elem ){}
    });  
    $('#home-news-wrapper').trigger( 'start' );       
}

function initArtGallery() {
    jQuery('#roundabout').roundabout({
        duration: 500,
        btnPrev: '#next',
        btnNext: '#prev'
    });

    /* overlays  */
    jQuery('.roundabout-moveable-item').hover(function(){
        jQuery(".item-content", this).stop().animate({
            opacity: 1
        },{queue:false,duration:300});
    }, function() {
        jQuery(".item-content", this).stop().animate({
            opacity: 0
        },{queue:false,duration:300});
    });
}    

function initServiceStations() {
    $(".article-select").change(function () {
        var url = $(this).val();
        $('#service-station-adress-inner').fadeOut('slow', function() { $(this).remove(); });
        $('#service-station-adress').html('<img style="position:absolute; top: 50%; left: 50%;" src="/images/ajax-loader.gif" />');                          
        $('#service-station-adress').load(url + " #service-station-adress-inner", null, function(){});
        return false;
    });
}   

function initMainMenu() {
    initAfterLoad();
    $('a.mainmenu').unbind();          
    $('a.mainmenu').click(function () {
        if(loading)
            return false;        
        var link = $(this);
        var url = link.attr('href');
        var fullurl = 'http://' + window.location.hostname + url;
        if(link.attr('box'))
            var target = link.attr('box');
        else
            var target = link.attr('id');
        $('.inner').fadeOut('fast',function() { $(this).remove(); });
        // hide submenu icon while scrolling
        $('.subnav').fadeOut();             
        setTimeout(function(){
            // scroll               
            $('#wrapper').scrollTo(target, 1500, {axis:'x'});
            setPosition(target, '#bg1', '0px', '400px', '800px', '1200px', '1600px', '2000px', '2400px', '2800px', '3200px', '3600px');
            setPosition(target, '#bg2', '0px', '1000px', '2000px', '3000px', '4000px', '5000px', '6000px', '7000px', '8000px', '9000px');
            setPosition(target, '#bg3', '0px', '600px', '1200px', '1800px', '2400px', '3000px', '3600px', '4200px', '4800px', '5400px', function(){
                loadContent(url, target);
            }); 
            // menu highliting             
            $('a.mainmenu, a.submenu').removeClass('selected');
            link.addClass('selected');

            // higlight contact
            if(link.attr('rel') == 'art379') 
                $("#art379").addClass('selected');                               
            // highlight downloads
            else if(link.attr('rel') == 'art378')    
                $("#art378").addClass('selected');                               
            // highlight catalogue
            else if(link.attr('rel') == 'art697')    
                $("#art697").addClass('selected');                               
            // highlight first subnav item            
            else    
                $(target + " a.submenu").last().addClass('selected');                               
        }, 300);
        return false;
    });                        
}  

function initSubMenu() {  
    $('a.submenu').click(function(){
        $($(this).attr('box') + ' .inner').fadeOut('fast', function() { $(this).remove(); });
        loadContent($(this).attr('href'), $(this).attr('box'));
        initAfterLoad();
        $('a.submenu').removeClass('selected');                        
        $(this).addClass('selected'); 
        return false;
    });
}    

function initJParallax() {  
    jQuery('#parallax .parallax-layer')
    .parallax({
        mouseport: jQuery('#parallax')
    }); 
}       

function initAfterLoad() { 

    $('#service-station-submit').remove(); 
    initServiceStations()                          

    Cufon.replace('h1, h1 p, #collection-subnav a');  

    Cufon.replace('#nav a', {
        hover: {
            color: '#fd9609'
        }
    });    

    Cufon.replace('#meta-nav a', { 
        hover: {
            color: '#ffffff'
        }    
    }); 

    Cufon.replace('.subnav-right a', {
        hover: {
            color: '#fd9609'
        }    
    });    

    $('.input-submit').hover(
      function () {
        $(this).addClass("input-submit-hover");
      },
      function () {
        $(this).removeClass("input-submit-hover");
      }
    );     

    $(".popup, .popup-link-with-arrow, .popup-link-without-arrow").fancybox({
        'width'              : 920,
        'height'             : 504,
        'autoScale'          : false,
        'padding'            : 1,
        'scrolling'          : 'auto',
        'type'               : 'iframe',
        'centerOnScroll'     : true,
        'overlayShow'        : false,
        'overlayColor'       : '#000',
        'overlayOpacity'     : 0.8,      
        'hideOnOverlayClick' : true,
        'overlayShow'        : true,        
        'margin'             : 0
    });           

    $(".image-popup, a[rel=group]").fancybox({
        'titleShow'     : false,
        'transitionIn'      : 'none',
        'transitionOut'     : 'none',
        'easingIn'      : 'easeOutBack',
        'easingOut'     : 'easeInBack',
        'overlayColor'       : '#000',
        'overlayOpacity'     : 0.8            
    });

    $("img.icon-main-nav").hover(function() {
        $(this).stop().attr ('src', '/images/v2-home.png').fadeIn(500);
    }, function() {
        $(this).stop().attr('src', '/images/v2-logo.png').fadeIn(500);
    });
    
    // opens the collection details
    $(".collection-detail").fancybox({
        'width'              : 920,
        'height'             : 504,
        'autoScale'          : false,
        'padding'            : 1,
        'scrolling'          : 'no',
        'type'               : 'iframe',
        'centerOnScroll'     : true,
        'overlayShow'        : true,
        'overlayColor'       : '#000',
        'overlayOpacity'     : 0.8,
        'hideOnOverlayClick' : true,
        'hideOnContentClick'  :   true,
        'titleShow'           : false,
        'margin'              : 0      
    });        

    $('#screen').serialScroll({
        target:'#sections',
        items:'td',
        prev:'img.prev',
        next:'img.next',
        axis:'x',
        navigation:'#date-navigation li a',
        duration:250,
        force:true,    
        start: 0,      
        cycle:false,
        onBefore:function( e, elem, $pane, $items, pos ){
            e.preventDefault();
            if( this.blur )
                this.blur();
             
            $('#date-navigation li a').removeClass('selected'); // all links
            $('#date-navigation li a#date-nav-' + pos).addClass('selected'); // clicked link               
                
            $('img.prev,img.next').show(); 
                              
            if( pos == 0 ) {
                $('img.prev').hide();
            }                                   
            else if( pos == $items.length-1 ) {
                $('img.next').hide(); 
            }    
            else if( $items.length == 1) {
                $('img.next').hide();  
            }                                                 
                               
        },
        onAfter:function( elem ){}
    });        

    $('#collection-screen').serialScroll({
        target:'#sections',
        items:'td',
        prev:'img.prev',
        next:'img.next',
        axis:'x',
        navigation:'#date-navigation li a',
        duration:250,
        force:true,    
        start: 0,
        cycle:false,
        onBefore:function( e, elem, $pane, $items, pos ){
            e.preventDefault();
            if( this.blur )
                this.blur();                    

            $('img.prev,img.next').show(); 

            if( pos == 0 ) {
                $('img.prev').hide();               
            }                                                          
            else if( pos == $items.length-5 ) {
                $('img.next').hide(); 
            }       
            else if( $items.length < 6 ) {
                $('img.next').hide();  
            }              
                              
        },
        onAfter:function( elem ){}
    }); 

}

// special function for FF on windows
// password saving resizes the window
var resizingEnabled = true;

function disableResize() { 
      resizingEnabled = false;    
}

function enableResize() { 
      resizingEnabled = true;    
}  

// Reload when resizing
/*
$(window).bind('resize',function(){
     // hide submenu icon while scrolling
     if(resizingEnabled) {
         window.location.href = 'http://www.fortis-watches.com/front_content.php?idart=271';
         $('.subnav').hide();           
     } 
     else {
         $('.subnav').show();           
     }              
});
*/

var mobileClients=[
"midp",
"240x320",
"blackberry",
"netfront",
"nokia",
"panasonic",
"portalmmm",
"sharp",
"sie-",
"sonyericsson",
"symbian",
"windows ce",
"benq",
"mda",
"mot-",
"opera mini",
"philips",
"pocket pc",
"sagem",
"samsung",
"sda",
"sgh-",
"vodafone",
"xda",
"iphone",
"ipod"
];
function isMobileClient(userAgent) {
    try {
        userAgent=userAgent.toLowerCase();
        for (var i=0; i < mobileClients.length; i++) {
            if (userAgent.indexOf(mobileClients[i]) != -1) {
                return true;
            }
        }
    }
    catch (e) { 
    }
    return false;
}
function isThisBrowserMobileClient() {
    return isMobileClient(navigator.userAgent);
}
function redirectIfMobileClient() {
    try {
        if (isThisBrowserMobileClient()) {
            if (document.location.href.indexOf('nomobile') != -1) {
                document.cookie='nomobile';
                return;
            }
            if (document.cookie && document.cookie.indexOf('nomobile') != -1) {
                return;
            }
            document.location.href="http://m.fortis-watches.com";
        }
    }
    catch (e) {
    }
}

