/**
 ******************** javascript functions ********************
 *
 * author :: sam zimmermann
 */



jQuery(document).ready(function($) {

     function getUrlVars()
     {
        var vars = [], hash;
        var hashes = window.location.href.slice(
                     window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
             hash = hashes[i].split('=');
             vars.push(hash[0]);
             vars[hash[0]] = hash[1];
        }
        return vars;
     }

     var intParam = getUrlVars()["printview"];
    if(intParam == 1) {window.print();}


    /**
     ******************** fontsize ********************
     *
     */
    var smaller = '0.9em';
    var normal = '1.1em';
    var bigger = '1.3em';

    // set fontsize
    $.fn.setFontSize = function( size )
    {
        $.cookie( 'fontsize', size, { expires: 1 } );
        $( '#content' ).css('font-size', $.cookie('fontsize') );
    };

    if( $.cookie('fontsize') != null )
    {
        $( '#content' ).css('font-size', $.cookie('fontsize') );
    } else
    {
        $( '#content' ).css('font-size', normal );
    }

    // add click-handlers
    $('.smaller').click(function() { $( '#content' ).setFontSize( smaller ); });
    $('.normal').click(function() { $( '#content' ).setFontSize( normal ); });
    $('.bigger').click(function() { $( '#content' ).setFontSize( bigger ); });
    
    /**
     ******************** navigation ********************
     *
     */
    var lastActiveItem = $( '#menu_level_last img.active' ).last();
    lastActiveItem.css( 'visibility', 'visible' );
    
    /**
     ******************** toggle configurator selector sections ********************
     *
     */ 
     
   /* $('#configuratorSelectorContent h2').click(function() { toggleConfiguratorSelectorContent(this); }); 
    $('#configuratorSelectorContent h3').click(function() { toggleConfiguratorSelectorContent(this); }); 
    
    
    function toggleConfiguratorSelectorContent(elm) {    
        var childElm = $(elm).parent().children('div');   
        var displayStatus = childElm.css('display'); 
        
        if(displayStatus == 'block') {      
            $(childElm).fadeOut('600');  
        } else {                              
            $(childElm).fadeIn('1000');  
        }                             
    }*/
    
    /**
     ******************** toggle configurator specifications sections ********************
     *
     */     
     $("#configurator div.selection").mouseenter(function() {            
        var productId =  $(this).attr('id').substring(17);    
        var elmImage = $('#productSelectionImage_'+productId);
        var elmContinue = $('#productSelectionContinue_'+productId);  
        
        showConfiguratorSpecification(elmImage, elmContinue, this); 
     }).mouseleave(function(){  
        var productId =  $(this).attr('id').substring(17);    
        var elmImage = $('#productSelectionImage_'+productId);
        var elmContinue = $('#productSelectionContinue_'+productId);        
        
        hideConfiguratorSpecification(elmImage, elmContinue, this);
     });  
     
     $("#configurator div.specification").mouseenter(function() {            
        var productId =  $(this).attr('id').substring(18);    
        var elmImage = $('#typeSpecificationImage_'+productId);
        var elmContinue = $('#typeSpecificationContinue_'+productId);  
        
        showConfiguratorSpecification(elmImage, elmContinue, this); 
     }).mouseleave(function(){  
        var productId =  $(this).attr('id').substring(18);    
        var elmImage = $('#typeSpecificationImage_'+productId);
        var elmContinue = $('#typeSpecificationContinue_'+productId);        
        
        hideConfiguratorSpecification(elmImage, elmContinue, this);
     }); 
     
     function hideConfiguratorSpecification(elmImage, elmContinue, elmContainer) {    
        $(elmContinue).stop(true, true).fadeOut(300);         
        $(elmImage).stop(true, true).hide('drop');           
        $(elmContainer).stop(true, true).css('background-color', '#ffffff');                                
     }    
     
     function showConfiguratorSpecification(elmImage, elmContinue, elmContainer) {    
        $(elmImage).stop(true, true).show('drop');        
        $(elmContinue).stop(true, true).fadeIn(600);       
        $(elmContainer).stop(true, true).css('background-color', '#ebebeb');                            
     } 
     
     /**
     ******************** toggle configurator functions sections ********************
     *
     */                                                                                   
     
     $("#configuratorFunctions div.function").stop(true, true).mouseenter(function() {
        var functionId =  $(this).attr('id').substring(20); 
        var elmContinue = $('#configuratorFunction'+functionId+'Continue');      
        
        showConfiguratorFunction(elmContinue, this); 
     }).stop(true, true).mouseleave(function(){                                 
        var functionId =  $(this).attr('id').substring(20); 
        var elmContinue = $('#configuratorFunction'+functionId+'Continue');             
        
        hideConfiguratorFunction(elmContinue, this);       
     }); 
     
     $("#configuratorFunctionsTop div.function").stop(true, true).mouseenter(function() {
        showConfiguratorFunction($('#configuratorBackContinue'), this); 
     }).stop(true, true).mouseleave(function(){
        hideConfiguratorFunction($('#configuratorBackContinue'), this);       
     });   
     
     function hideConfiguratorFunction(elmContinue, elmFunction) {    
        $(elmContinue).stop(true, true).fadeOut(200); 
     }  
     
     
     function showConfiguratorFunction(elmContinue, elmFunction) {                
        $(elmContinue).stop(true, true).fadeIn(500);                                                                       
     } 

    /**
     ******************** growmedia.ca :: fullscreen background ********************
     *
     */

    // adjust image size
    $.fn.resizenow = function() {
        //Define starting width and height values for the original image
        var startwidth = 1280; // 1440
        var startheight = 800; // 956
        //Define image ratio
        var ratio = startheight/startwidth;
        //Gather browser dimensions
        var browserwidth = $(window).width();
        var browserheight = $(window).height();
        //Resize image to proper ratio
        if ((browserheight/browserwidth) > ratio) {
            $(this).height(browserheight);
            $(this).width(browserheight / ratio);
            $(this).children().height(browserheight);
            $(this).children().width(browserheight / ratio);
        } else {
            $(this).width(browserwidth);
            $(this).height(browserwidth * ratio);
            $(this).children().width(browserwidth);
            $(this).children().height(browserwidth * ratio);
        }
        //Make sure the image stays center in the window
        $(this).children().css('left', (browserwidth - $(this).width())/2);
        $(this).children().css('top', (browserheight - $(this).height())/2);

    };

    // init functions
    $('#supersize').resizenow();
    $(window).resize(function () { $('#supersize').resizenow(); });


    /**
     ******************** accordion ********************
     *
     */

    $.fn.rotateBullet = function( bullet )
    {
        var bgStatus = $( bullet ).css('background-position' );
        if( bgStatus == '0px -24px' )
        {
            $( bullet ).css('background-position', '0px -2px' );
        } else
        {
            $( bullet ).css('background-position', '0px -24px' );
        }

    };

    $.fn.openBullet = function( pContent, pBullet)
    {
        //console.log( bullet + 'open' );
        $( pContent ).css( 'display', 'block' );
        $( pBullet ).css('background-position', '0px -2px' );
    };

    // switch accordion bullet
    $( '#accordion>a' ).click(function()
    {
        // open accordion
        $(this).next().slideToggle('fast');
        // rotate bullet
        var openerDiv = $(this).children('div');
        $( '#accordion' ).rotateBullet( openerDiv );
    })

    // open bullet for shown content
    var openContent = $('#accordion a.open').next();
    var openBullet = $('#accordion a.open div');

    $( '#accordion div.open' ).openBullet( openContent, openBullet );



    /**
     ******************** blend next and prev arrows ********************
     *
     */
    $('.next_slide')
            .hover(function() {
        $('#slider_nav')
                .stop(true, true)
                .fadeIn('slow');
    }, function() {
        $('#slider_nav')
                .stop(true, true)
                .fadeOut('slow');
    }
    );

    $('.prev_slide')
            .hover(function() {
        $('#slider_nav')
                .stop(true, true)
                .fadeIn('slow');
    }, function() {
        $('#slider_nav')
                .stop(true, true)
                .fadeOut('slow');
    }
    );

    /**
     ******************** jquery cycle plugin ********************
     *
     */
    var speedCf     = 800;                                             // time of crossfade
    var timeoutCf   = 10000;                                           // timeout between slides

    $( '#supersize' )
        //.after( '<div id="pager">' )
        .cycle(
        {
            fx:         'fade',                                         // transition style
            speed:      speedCf,                                        // time of crossfade
            timeout:    timeoutCf,                                      // timeout between slides
            prev:       '.prev_slide',                                  // button for previous img
            next:       '.next_slide'                                   // button for previous img
           

        });

    $( '#txt_home' )
        //.after( '<div id="pager">' )
        .cycle(
        {
            fx:         'fade',                                         // transition style
            speed:      speedCf,                                        // time of crossfade
            timeout:    timeoutCf,                                      // timeout between slides
            prev:       '.prev_slide',                                  // button for previous img
            next:       '.next_slide'                                   // button for previous img
        });

});







