﻿$(document).ready(function() {

    var timer;
    $('td.qty input.textbox').click(function() {
        if ($(this).val() == ""){
            $(this).val("1");
        }
    })
    // Hide anything that needs to appear for non-java script users
    if ($('#jsHide').length > 0) {
        $('#jsHide').hide();
    }

    var $contheight = $('#wrapper').height() - 69;
    $('#product-nav').css({ top: $contheight + 'px' });

    // Show anything that needs to appear for java script users
    $('.jsShow').show();

    $('.magnifyswatch').height('30px');
    $('.nomagnifyswatch').height('30px');

    $('#product-downloads').attr("z-index", "500").slideToggle(0);

    $('#product-download').click(function() {
        $('#product-downloads').slideToggle(500);
        return false;
    })

    var $rand = Math.floor(Math.random() * 3);

    //    $('#slideshow-images').cycle({
    //        fx: 'fade', //scrollHorz
    //        pause: 0,
    //        speed: 4000,
    //        timeout: 5000,
    //        random: 0,
    //        next: '#next',
    //        startingSlide: $rand
    //    });

    //    $('#slideshow-intros').cycle({
    //        fx: 'fade',
    //        pause: 0,
    //        speed: 4000,
    //        timeout: 5000,
    //        random: 0,
    //        next: '#next',
    //        startingSlide: $rand
    //    });

    //    $('#slideshow-icons').cycle({
    //        fx: 'fade',
    //        pause: 0,
    //        speed: 4000,
    //        timeout: 5000,
    //        random: 0,
    //        next: '#next',
    //        startingSlide: $rand
    //    });

    if ($('.prod-gallery').length > 0) {
        var galleries = $('.prod-gallery').adGallery();
        galleries[0].settings.description_wrapper = $('#mainSlideIntro');
    }

    if ($('.film-gallery').length > 0) {
        var galleries = $('.film-gallery').adGalleryFilm();
        galleries[0].settings.description_wrapper = $('#mainSlideIntro');
    }

    //    $('#area-slideshow #area-slideshow-images').cycle({
    //        fx: 'fade',
    //        pause: 0,
    //        speed: 3000,
    //        timeout: 5000,
    //        random: 0,
    //        next: '#area-slideshow-next',
    //        prev: '#area-slideshow-prev'
    //    });

    $('.ad-image-wrapper').hover(function() {
        if ($(this).find('.Image').attr('src') != null) {
            $('#magnify-image').show();
        }
    }, function() {
        $('#magnify-image').hide();
    });

    $('.ad-image-wrapper').click(function() {

        if ($(this).find('.Image').attr('src') != null) {

            $('#blackout').show().css({ 'opacity': 0.7 });
            $('#largeimagemodal').fadeIn();
            $('#largeimagecontainer').html('<img src="' + $(this).find('.Image').attr('src').replace('product_', '') + '" style="border: solid 10px #FFF" />');

            var img = new Image();
            $(img).load(function() {
                var h = $('#largeimagecontainer img').height() + 20;
                var b = $(window).height();

                var top = (b - h) / 2;

                if (top < 0) {
                    $('#largeimagemodal').css({ 'top': '10px' });
                    $('#largeimagecontainer').height(b - 80 + 'px');
                    $('#largeimagecontainer img').height(b - 100 + 'px');
                }
                else {
                    $('#largeimagemodal').css({ 'top': '10px' });
                    $('#largeimagecontainer').height('auto');
                }
            }).attr('src', $(this).find('.Image').attr('src').replace('product_', ''));
        }

        if ($(this).find('.Video').attr('longdesc') != null) {
            alert($(this).find('.Video').attr('longdesc'));
        }

        if ($(this).find('.Flash').attr('longdesc') != null) {
            window.open('/360_html/' + $(this).find('.Flash').attr('longdesc') + '.html', '', 'status=yes,height=740');
        }
    });

    $('#largeimagemodal').click(function() {
        $(this).fadeOut();
        $('#blackout').fadeOut();
    });

    $('.magnifyswatch').hover(function() {
        var id = $(this).attr('id').substring(11);

        $('.swatchimagegallery').hide();

        if ($('#swatchGallery' + id + ' ul').children().length > 0) {

            $('#swatchGallery' + id).show().css('top', $(this).position().top);
            $('#swatchGallery' + id + ' ul li:first-child .Image').trigger('click');
        }
    }, function() {

    });

    $('.swatchimagegallery').hover(function() {
        $(this).show();
    }, function() {
        $('.swatchimagegallery').hide();
    });

    $('.swatchimagegallery .Image').click(function() {
        $('#showswatchimage').html('<img class="Image" src="' + $(this).attr('href') + '" />');

        var img = new Image();
        $(img).load(function() {
            var h = $('#showswatchimage img').height();

            if (h > 395) {
                $('#showswatchimage img').height('395px');
            }
        }).attr('src', $(this).attr('href'));

        return false;
    });

    // Load without caching
    $.fn.loadWithoutCache = function() {
        var elem = $(this);
        var func = arguments[1];
        $.ajax({
            url: arguments[0],
            cache: false,
            dataType: "html",
            success: function(data, textStatus, XMLHttpRequest) {
                elem.html(data);
                if (func != undefined) {
                    func(data, textStatus, XMLHttpRequest);
                }
            }
        });
        return elem;
    }



    $('#enquiry-cart').loadWithoutCache('/cart.aspx', function() {
    });

    $('a#cart').click(function() {
        toggleBasket();
        return false;
    });


    $('#login').loadWithoutCache('/login.aspx', function() {
    });

    $('a.login').click(function() {
        toggleLogin();
        return false;
    });

    //    $('#Strada3DHolder').loadWithoutCache('/strata3d.aspx?fileOne=betatrak-plug.jar&fileTwo=cmd_grad.jar', function() {
    //    });

    //    $('a#view3D').click(function() {
    //        toggle3d();
    //        return false;
    //    });


    // Free search text box
    // Set default value if blank
    if ($("#freesearchtext").val() == '') {
        $("#freesearchtext").val('Search All Products');
    }
    // On focus, select text
    $("#freesearchtext").focus(function() {
        if ($("#freesearchtext").val() == 'Search All Products') {
            $(this).val('');
        }
        else {
            $(this).select();
        }
    });
    $("#freesearchtext").blur(function() {
        if ($("#freesearchtext").val() == '') {
            $(this).val('Search All Products');
        }
    });



});

function displayStrada(url) {
    $('#Strada3DHolder').loadWithoutCache("/strata3d.aspx?" + url, function() {
}).fadeIn(500);
    
toggle3d();
}

function toggleBasket() {
    $left = $('#contentStyle').position().left;
    $leftpos = $left + 223;
    $('#enquiry-cart').css('left', $leftpos + 'px');
    $('#enquiry-cart').slideToggle(250);
    //$('#basket-nav').toggleClass('selectedMenu');
}

function toggleLogin() {
    $left = $('#contentStyle').position().left;
    $leftpos = $left + 545;
    $('#login').css('left', $leftpos + 'px');
    $('#login').slideToggle(250);
    //$('#basket-nav').toggleClass('selectedMenu');
}

function toggle3d() {
    $left = $('#contentStyle').position().left;
    $leftpos = $left - 0;
    $('#Strada3D').css('left', $leftpos + 'px');
    $('#Strada3D').fadeIn();
}

jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel();
    jQuery('#imgThumbsCarousel').jcarousel();
    jQuery('#productRangeCarousel').jcarousel();
});

jQuery(document).ready(function() {
    $('.extraImage').click(function() {
        clearTimeout(timer);
        var img = $(this).attr('href');
        $('#mainImg').attr('src', img);
        var largeimg = img.replace('product_', 'large-product_');
        $('#magnify a').attr('href', largeimg);
        return false;
    });

    jQuery('.main-img').ready(function() {
        setTenSeconds()
    })
    
    function switchImages() {
        var change = false;
        var changed = false;
        //loop all the thumbnails
        $('.extraImage').each(function() {
            // if the change flag is set and the image hasn't already changed, change the image.
            if (!changed && change) {
                var img = $(this).attr('href');
                $('#mainImg').attr('src', img);
                var largeimg = img.replace('product_', 'large-product_');
                $('#magnify a').attr('href', largeimg);
                change = false;
                changed = true;
            }
            // check at the end of the looped item if it is the current image and set the change flag for next time through
            if ($(this).attr('href') == $('#mainImg').attr('src')) {
                change = true;
            }
        })
        // if the loop finished without changing an image, the current image must be the last. so go back to the first image.
        if (!changed) {
            var img = $('.extraImage').first().attr('href');
            $('#mainImg').attr('src', img);
            var largeimg = img.replace('product_', 'large-product_');
            $('#magnify a').attr('href', largeimg);
        }
        setTenSeconds();

    }
    function setTenSeconds() {
        timer = setTimeout(function() {
            switchImages();
        }, 7000)
    }


    $('#magnify').click(function() {
        var img = $('#magnify a').attr('href');
        $('#largeImg').attr('src', img);
        $('#flyoutImg').fadeIn();
    });


    $('#close3D').click(function() {
        $('#Strada3D').fadeOut();
    });

    $('#imgClose').click(function() {
        $('#flyoutImg').fadeOut();
    });

    $('.review-list').height('340px')

    //    $('#int-panel-tabs').cycle({
    //        fx: 'fade',
    //        pause: 20,
    //        speed: 4000,
    //        random: 1,
    //        prev: '.prev',
    //        next: '.next'
    //    });

    $(function() {
        var tabContainers = $('#int-panel-tabs > div.tabHolder');
        tabContainers.hide();
        $('#panel-nav a').click(function() {
            tabContainers.hide().filter(this.hash).fadeIn(1099);
            $('.default').hide();
            $('panel-nav a').removeClass('selected');
            $(this).addClass('selected');

            return false;
        });

        var randomnumber = Math.floor(Math.random() * 6);
        var i = 0;
        $('#panel-nav a').each(function() {
            if (randomnumber == i && i > 0) {
                $(this).click();
                //alert(randomnumber);
            }
            i++;
        });		
    });

    $('#default').click(function() {
        $('.default').fadeIn(1099);
    });

    $('#address-bar').hide();

    $('.locationMarker').click(function() {
        $('.location').hide();
        var thisId = $(this).attr('id');
        var newId = thisId.replace('Flag-', '');
        //alert(newId);
        $('#' + newId).toggle();
        return false;
    });

    $('.closeMap').click(function() {
        $('.location').hide();
    });


    if ($("#tweet").length > 0) {
        $("#tweet").tweet({
            username: "CMD_Ltd",
            join_text: "auto",
            avatar_size: 0,
            count: 2,
            auto_join_text_default: "we said,",
            auto_join_text_ed: "we",
            auto_join_text_ing: "we were",
            auto_join_text_reply: "we replied to",
            auto_join_text_url: "we were checking out",
            loading_text: "loading tweets..."
        });
    }



});


function moreReviews(ammount) {
    var size = ammount * 100;
    $('.review-list').animate({
           height: (size+45)+'px'
        }, 1000, function() {
            // Animation complete.
        });
    }


    $open = 0;
    function trainingEnquiry(id) {
        if ($open == false) {
            $('#Q-' + id).animate({
                height: (620) + 'px'
            }, 1000, function() {
                // Animation complete.
            $open = 1;
            });
        }
        else {
            $('#Q-' + id).animate({
                height: (150) + 'px'
            }, 1000, function() {
            // Animation complete.
            $open = 0;
            });
        }
    }


//    $flyoutopen = 0;
//    function cart() {
//        if ($flyoutopen == 1) {
//            //$('#cart').css('display', 'none');
//            $('#enquiry-cart').slideUp(250);
//            $flyoutopen = 0;
//        }
//        else {

//            $left = $('#header').position().left;
//            $leftpos = $left + 223;
//            $('#enquiry-cart').css('left', $leftpos + 'px');

//            $('#enquiry-cart').slideDown(250);
//            $flyoutopen = 1;
//        }

    //    }


function getStrata(stradaOne, StradaTwo) {
    var code = "";
    code = "<applet code=com.kaon.meson.MesonApplet archive='mesonApplet.jar,rasterGluon.jar,sceneGluon.jar' width=400 height=375 alt='Please Enable Java' codebase='/Library/Default/Documents/' MAYSCRIPT><param name='brand' value='strata'><param name='meson' value='/* Copyright 2004-2006, Kaon Interactive Inc. */  /* Note: to include an apostrophe in this section, use \u0027 */  Applet.Primary.fill=\"ffffff\"; /* Background color goes here */ Meson.setCurrentDataSource(Meson.documentBase=Meson.codeBase);  Meson.gluon(\"com.kaon.meson.raster.RasterGluon\");  Meson.gluon(\"com.kaon.meson.scene.SceneGluon\"); Image.Loading.anchor=_C;  Image.Loading.x={Display.Primary.width/2};   Image.Loading.y={Display.Primary.height/2};  Image.Loading.text=\"Initializing...\";   makeControl={  #u=Meson.unique();  \"AppControl.\"~#u~.actionType=\"anim\";  \"AppControl.\"~#u~.script=#script;  \"AppControl.\"~#u~.subGroup=#subGroup;  \"I18N.Class.\"~#u='AppGraphic.\"~#u; \"AppGraphic.\"~#u~.text=#text;  \"AppGraphic.\"~#u~.tipText=#tipText;  };   makeHotSpot={   #u=Meson.unique();  \"AppHotSpot.\"~#u~.location=#location;  \"AppHotSpot.\"~#u~.script=#script;  \"I18N.Class.\"~#u='AppGraphic.\"~#u;  \"AppGraphic.\"~#u~.text=#text;  };  scriptButtonsSpace=0;";
    code += "Data.Program.model=\"" + stradaOne + "\";";
    code += "Data.Program.url=\"" + StradaTwo + "\";";
    code += "customizeUI={ Shadow.Shadow_3d.alpha=0.75; /* Set to 0.0 for no shadow */  uiDone();  }; '>Java is currently not available on your browser. Please enable Java (www.java.com) to view the model.</applet>";
    document.getElementById('Strada3D').innerHTML = code;
}

function getWarning(step, value) {
    switch(step)
    {
        case 'depth':
            if (value != 2) {
                $('#' + step + 'Warning').show();
                $left = $('#' + step + 'Q').position().left;
                $leftpos = $left - 230;
                $('#' + step + 'Warning').css('left', $leftpos + 'px');
                $top = $('#' + step + 'Q').position().top;
                $('#' + step + 'Warning').css('top', ($top + 45) + 'px');
            }
            else 
            {
                $('#' + step + 'Warning').hide();
            }
            break;
        case 'tapoff':
            if (value != 3) {
                $('#'+step+'Warning').show();
                $left = $('#' + step + 'Q').position().left;
                $leftpos = $left - 230;
                $('#' + step + 'Warning').css('left', $leftpos + 'px');
                $top = $('#' + step + 'Q').position().top;
                $('#' + step + 'Warning').css('top', ($top + 45) + 'px');
            }
            else {
                $('#' + step + 'Warning').hide();
            }
            break;
        case 'socketSpace':
            $('#' + step + 'Warning').show();
            $left = $('#' + step + 'Q').position().left;
            $leftpos = $left - 230;
            $('#' + step + 'Warning').css('left', $leftpos + 'px');
            $top = $('#' + step + 'Q').position().top;
            $('#' + step + 'Warning').css('top', ($top) + 'px');
            break;
        case 'accessories':
            if (value == 1 || value == 2 || value == 3 || value == 4 || value == 13 || value == 14 || value == 15 || value == 16) {
                $('#' + step + 'Warning').show();
                $left = $('#' + step).position().left;
                $leftpos = $left - 230;
                $('#' + step + 'Warning').css('left', $leftpos + 'px');
                $top = $('#' + step).position().top;
                $('#' + step + 'Warning').css('top', ($top - 50) + 'px');
            }
            else {
                $('#' + step + 'Warning').hide();
            }
            break;    
        default:
            $('#' + step + 'Warning').hide();
            
            
            
    }
}

