function imagePreview() {
    $('.jcarousel-item img, .jcarousel-item a').bind("click", function (e) {
        var img_src = $(this).attr('src').replace(/([^.]*)(_thumb)(.*)/, "$1_$3"); //img_imgViewer_thumb1
        var $img = $('<img />').attr({
            'id': 'img_ad',
            'class': 'img_block',
            'src': img_src
        });

        $('img#img_ad').replaceWith($img);
        $('img#img_ad').parent('a').attr('href', img_src);

        tb_init('a.thickbox');
        //e.stopPropagation();
        return false;
    });
}

// page tools
function checkSlider() {
    if ($.cookie('slider') != null) {
        if ($.cookie('slider') == 'closed') {
            $('#page_tools').css('background', 'url(/images/bg_page_tools_on.gif)');
            $('#page_tools').width('25px');
        }
    }
}

//solution image rotation
var imgIndexCurrent = 0;
var imgIndex = new Array(0, 0, 0, 0, 0);

//font size
var currentSizeIndex = 1;
var maxSizeIndex = 5;
var sizeIncrement = 0.2;

var fontSizeHeaderTitle = 1.7;
var fontSizeHeaderIntro = 1;

var fontSizeBody = 1;
var sizeIncrementBody = 0.1;

var fontSizeBreadcrumbs = 100;
var sizeIncrementBreadcrumbs = 5;

function rotationSolutionHome() {
    var solutionImages = $('.solutionCategorys .sub ul li div.image');
    if (solutionImages.length > 0) {

    }
}

function rotateSolution(pnlImage, indexNumber) {
    var nxtImageIndex = 0;

    var thumbnail_length = parseInt(eval('thumbnails_' + indexNumber + '.length'));
    var currentIndex = imgIndex[indexNumber];

    var currentthumbnail = eval('thumbnails_' + indexNumber + '[' + imgIndex[indexNumber] + ']');
    var currentthumbnailtitle = eval('thumbnails_title_' + indexNumber + '[' + imgIndex[indexNumber] + ']');

    var nextthumbnail = "";

    if (thumbnail_length > 1) {

        if ((currentIndex + 1) < thumbnail_length) {
            nxtImageIndex = currentIndex + 1;
        }

        nextthumbnail = eval('thumbnails_' + indexNumber + '[' + nxtImageIndex + ']');
        nextthumbnailtitle = eval('thumbnails_title_' + indexNumber + '[' + nxtImageIndex + ']');

        $(pnlImage).fadeTo(500, 0, function () {
            $(pnlImage).find('img').attr({ src: nextthumbnail});
            imgIndex[indexNumber] = nxtImageIndex;
            $(pnlImage).fadeTo(3000, 1);
        });



        setTimeout("rotateSolution('" + pnlImage + "'," + indexNumber + ")", 10000);
    }
}

$(document).ready(function () {

    //homepage 
    $(document).ready(function () {
        $("ul.sf-menu").superfish();
    });

    // Tabs
    $('#tabs').tabs();

    //hover states on the static widgets
    $('#dialog_link, ul#icons li').hover(
		function () { $(this).addClass('ui-state-hover'); },
		function () { $(this).removeClass('ui-state-hover'); }
	);

    //Dealer list on RHS - sliding up and down
    $('.dealer_container a.arrow_down').live("click", function (e) {
        $(this).parent().find("ul").slideDown("slow");
        $(this).removeClass().addClass("arrow_up");
        $(this).html('Hide Products');
    });

    $('.dealer_container a.arrow_up').live("click", function (e) {
        $(this).parent().find("ul").slideUp();
        $(this).removeClass().addClass("arrow_down");
        $(this).html('Show Products');
    });

    $('.question.arrow_down').live("click", function (e) {
        $(this).parent().find(".answer").show(200);
        $(this).removeClass("arrow_down").addClass("arrow_up");
    });

    $('.question.arrow_up').live("click", function (e) {
        $(this).parent().find(".answer").hide(200);
        $(this).removeClass("arrow_up").addClass("arrow_down");
    });

    /*
    $('#carousel_featured_product').jcarousel({
    vertical: true,
    scroll: 2
    });
    */

    //$('#carousel_related_product').jcarousel();
    //$('#carousel_imgViewer').jcarousel();

    $('.live_music img').click(function () {
        //alert("test");
        $(this).parent("div").addClass("music_ov").siblings().removeClass("music_ov");
        $('.box_brand').css('display', 'block');

        $('.main_section').mouseleave(function () {
            $('.box_brand').css('display', 'none');
        });
    });

    $('.viewAllNewsDown').live('click', function (e) {
        $(this).parent().find("ul.subItems").slideDown("slow");
        $(this).removeClass("viewAllNewsDown").addClass("viewAllNewsUp");
        $(this).html("&raquo;&nbsp;Hide news articles");
    }
           );

    $('.viewAllNewsUp').live('click', function (e) {
        $(this).parent().find("ul.subItems").slideUp("slow");
        $(this).removeClass("viewAllNewsUp").addClass("viewAllNewsDown");
        $(this).html("&raquo;&nbsp;View all news articles");
    }
           );

    $('.viewAllEventsDown').live('click', function (e) {
        $(this).parent().find("ul.subItems").slideDown("slow");
        $(this).removeClass("viewAllEventsDown").addClass("viewAllEventsUp");
        $(this).html("&raquo;&nbsp;Hide events");
    }
           );

    $('.viewAllEventsUp').live('click', function (e) {
        $(this).parent().find("ul.subItems").slideUp("slow");
        $(this).removeClass("viewAllEventsUp").addClass("viewAllEventsDown");
        $(this).html("&raquo;&nbsp;View all events");
    }
           );

    $('.viewPendingUpdateDown').live('click', function (e) {
        $('#pendingUpdates').slideDown("slow");
        $(this).removeClass("viewPendingUpdateDown").addClass("viewPendingUpdateUp");
        $(this).html("&raquo;&nbsp;Hide pending changes");
    }
       );

    $('.viewPendingUpdateUp').live('click', function (e) {

        $('#pendingUpdates').slideUp("slow");
        $(this).removeClass("viewPendingUpdateUp").addClass("viewPendingUpdateDown");
        $(this).html("&raquo;&nbsp;View pending changes");
    }
       );

    /*
    $('#slider').live('click',function(e)
    {
    if ($('#page_tools').css('width')!='25px')
    $('#page_tools').animate({width:'25px'},150,'',function(){
    $('#page_tools').css('background','url(/images/bg_page_tools_on.gif)');
    $.cookie('slider','closed');
    });
    else
    $('#page_tools').animate({width:'120px'},150,'',function(){
    $('#page_tools').css('background','url(/images/bg_page_tools.gif)');
    $.cookie('slider','open');
    });
    }
    );
    */

    if ($.cookie('currentSizeIndex') != null) {
        currentSizeIndex = parseInt($.cookie('currentSizeIndex'));
    }
    else {
        $.cookie('currentSizeIndex', currentSizeIndex);
    }


    $('#resetfont').live('click', function (e) {

    }
    );

    //increase font
    $('#increasefont').live('click', function (e) {
        if (currentSizeIndex < maxSizeIndex) {
            currentSizeIndex = currentSizeIndex + 1;
            $.cookie('currentSizeIndex', (currentSizeIndex));

            setFontsize();
        }
    }
    );

    //decrease font
    $('#decreasefont').live('click', function (e) {
        if (currentSizeIndex > 1) {
            currentSizeIndex = currentSizeIndex - 1;
            $.cookie('currentSizeIndex', (currentSizeIndex));

            setFontsize();
        }
    }
    );


    //SETTING HEIGHTS FOR INTERNAL CONTENT//
    setHeightMainContent();
    setHeightSolutions();
    //setHeightSolutionBrands();

    //NEWS SUMMARY PAGE //
    //setFeaturedNewsWidth();

    //PRODUCT DETAIL TABS//
    setTab('.productTabs');

    //PRODUCT THUMBNAIL CONTAINER HEIGHTS//
    setProductThumbnailContainerHeight();

    setFontsize();

    //SITE MAP AND ADVANCED SEARCH DROP DOWNS//
    initAdvancedSearch();
    //initSitemap();


    //checkSlider();

    //PRODUCT DETAIL SLIDE SHOW//
    checkProductImageGallery();

});

function hideThankyou() {
    var thankyoudiv = $('div.thankyou');

    if (thankyoudiv.length > 0) {
        thankyoudiv.hide(200);
    }
    return true;
}

function setFeaturedNewsWidth() {
    var featuredDiv = $('.news_feature div.leftCol');

    if (featuredDiv.length > 0) {
        var featuredImage = featuredDiv.find('img');

        if (featuredImage.length > 0) {
            featuredDiv.width(featuredImage.width() + 10);
        }
    }
}

function setHeightMainContent() {
    var leftColHeight = $('.leftCol').height();
    var mainColHeight = $('.mainCol').height();

//    if (leftColHeight > mainColHeight)
//        $('.mainCol .content').height(leftColHeight - $('.mainCol .status').height() - $('.mainCol .topnav').height() - 20);
}

function setHeightSolutions() {
    var solutionSummaryli = $('#solutionSummary li');

    var maxHeightTitle = 0;
    var maxHeightImage = 0;
    var maxHeightContent = 0;

    if (solutionSummaryli.length > 0) {
        solutionSummaryli.each(function () {

            var title = $(this).find('h3');
            if (title.height() > maxHeightTitle)
                maxHeightTitle = title.height();

            var content = $(this).find('p');
            if (content.height() > maxHeightContent)
                maxHeightContent = content.height();

        });

        //set heights to be equal
        $(solutionSummaryli).find('h3').height(maxHeightTitle);
        $(solutionSummaryli).find('p').height(maxHeightContent);
    }
}

function setHeightSolutionBrands() {
    var currentHeight = 0;
    var nextHeight = 0;
    var solutionBrands = $('.solutionBrandSummaryContainer');

    //alert(solutionBrands.length);

    if (solutionBrands.length > 0) {
        solutionBrands.each(function (index) {


            //alert($(this).height() + ', ' + index)

            if (index % 2 == 0) {

                currentHeight = $(this).height() + 10;
                //alert("Left Cell " + $(this).find('h3 a').html() + " Height: " + currentHeight);

                //check next element
                if ((index + 1) < solutionBrands.length) {
                    nextHeight = solutionBrands.eq(index + 1).height() + 10;

                    //alert("Right Cell " + solutionBrands.eq(index + 1).find('h3 a').html() + " Height: " + nextHeight);

                    if (currentHeight >= nextHeight) {
                        $(this).height(currentHeight);
                        solutionBrands.eq(index + 1).height(currentHeight);
                    }
                    else {
                        $(this).height(nextHeight);
                        solutionBrands.eq(index + 1).height(nextHeight);
                    }
                }
                else {
                    $(this).height(currentHeight);
                }
            }

        });
    }
}

/* *************** */
/* PRODUCT DETAILS */
function setProductThumbnailContainerHeight() {
    var maxHeight = 0;

    var thumbnailContainers = $('.thumbnailImages ul li');

    if (thumbnailContainers.length > 0) {
        thumbnailContainers.each(function () {

            if ($(this).height() > maxHeight)
                maxHeight = $(this).height();
        });

        thumbnailContainers.height(maxHeight);
    }
}

function checkProductImageGallery() {

    var heroImage = $('div.mainImage a');
    var thumbnails = $('div.thumbnailImages ul li a.thumbnail');

    if (thumbnails.length > 0) {
        thumbnails.click(function () {

            var mediumImage = $(this).siblings('a.medium');
            if (mediumImage.length > 0) {
                heroImage.find('img').attr({ 'src': $(mediumImage).attr('href') });
            }
        })
    }
}

//product slide show
function showslide(indexNumber) {
    imgIndexCurrent = indexNumber;

    var zoomLink = $('div.mainImageContainer a.thickbox.zoom');
    var mainLink = $('div.mainImage a');

    if (zoomLink.length > 0) {
        $(zoomLink).attr('href', '/#TB_inline?height=412&width=600&inlineId=slideshow_full');
        $(mainLink).attr('href', '/#TB_inline?height=412&width=600&inlineId=slideshow_full');

        $('#fullsize_img').attr('src', thmbSlideshow[indexNumber][1]);
        $('.slideshow_fullcontent p').html('Image ' + (indexNumber + 1) + ' of ' + thmbSlideshow.length);
    }

    //$('#img_ad').find('img').attr('src', thmbSlideshow[indexNumber][0]);
    //$('#img_ad').find('a').attr('href', '/#TB_inline?height=395&width=565&inlineId=slideshow_full');
    //$('#zoomin').parent('a').attr('href', '/#TB_inline?height=395&width=565&inlineId=slideshow_full');
    //$('#fullsize_img').attr('src', thmbSlideshow[indexNumber][1]);
    //$('.slideshow_fullcontent p').html('Image ' + (indexNumber + 1) + ' of ' + thmbSlideshow.length);
}

function slideMove(action) {
    var moveIndex = 0;

    if (action == 'previous') {
        if (imgIndexCurrent == 0) {
            moveIndex = (thmbSlideshow.length - 1);
        }
        else
            moveIndex = imgIndexCurrent - 1;
    }
    else if (action == 'next') {
        if (imgIndexCurrent < (thmbSlideshow.length - 1)) {
            moveIndex = imgIndexCurrent + 1;
        }
        else {
            moveIndex = 0;
        }
    }

    showslide(moveIndex);
}


function setTab(tabContainer) {

    var tabs = $(tabContainer).find('.tabs ul.menu li');

    if (tabs.length > 0) {

        tabs.eq(0).addClass('active');

        var tabContents = $(tabContainer).find('.tab_content > div');
        tabContents.hide();

        tabContents.eq(0).show()

        var brochureLink = $('#hlBrochure');
        if (brochureLink.length > 0) {
            $(brochureLink).bind('click', function () {

                $(tabs).find('a[href=#tabs-download]').click();

                return false;
            });
        }

        $(tabs).each(function (index) {

            var anchor = $(this).find('a').eq(0);

            anchor.bind('click', function () {

                //turn off old
                $(".tabs ul li[class='active']").removeClass('active');
                $(tabContainer).find('.tab_content > div').hide();

                //turn on new
                tabs.eq(index).attr('class', 'active');

                var tabContents = $(anchor.attr('href'));
                //alert(tabContents.length);

                if (tabContents.length > 0) {
                    tabContents.show();
                }

                return false;
            })
        });
    }
}
/* *************** */

function setFontsize() {
    //sizes that do not change
    $('#tabs ul li a').css('font-size', '13px');

    if (currentSizeIndex > 1) {
        $('.content p').css('font-size', (fontSizeBody + (sizeIncrementBody * currentSizeIndex)) + 'em');
        $('.breakcrumb').css('font-size', (fontSizeBreadcrumbs + (sizeIncrementBreadcrumbs * currentSizeIndex)) + '%');
    }
    else {
        $('.content p').css('font-size', fontSizeBody + 'em');
        $('.breakcrumb').css('font-size', fontSizeBreadcrumbs + '%');
    }
}

function initSitemap() {
    var menu_ul_all = $('ul.sitemap li ul');
    $(menu_ul_all).hide();

    //sub1 items
    $('ul.sitemap li').each(function () {
        if ($(this).children("ul").length > 0) {
            $('span.more', this).addClass('menu_off');
        }
        else {
            $('span.more', this).removeClass('menu_off');
        }
    });

    //sub 2 items
    $('ul.sitemap>li>ul>li').each(function () {
        if ($(this).children("ul").length > 0) {
            $('span.more', this).addClass('menu_off');
        }
        else {
            $('span.more', this).removeClass('menu_off');
        }
    });

    $('ul.sitemap span.more').bind("click", function () {

        var toggleClass = $(this).attr("class");

        if (toggleClass == 'more menu_on') {
            $(this).attr('class', 'more menu_off').parent('div').siblings('ul').fadeOut('fast');
        }
        else if (toggleClass == 'more menu_off') {
            $(this).attr('class', 'more menu_on').parent('div').siblings('ul').fadeIn('fast');
        }
    });


}

function initAdvancedSearch() {

    var selectAll = $('#selectAll :checkbox');

    if (selectAll.length > 0) {
        selectAll.click(function () {
            var checkboxes = $('#advanced_search ul.advancedSearch :checkbox');

            if (checkboxes.length > 0) {

                if ($(this).is(':checked'))
                    checkboxes.attr('checked', 'true');
                else
                    checkboxes.attr('checked', '');
            }
        });
    }

    var checkboxes = $('#advanced_search ul.advancedSearch :checkbox');
    if (checkboxes.length > 0) {
        $(checkboxes).click(function () {
            var subCheckBoxes = $(this).parent().parent().find('ul li :checkbox');

            if ($(this).is(':checked'))
                subCheckBoxes.attr('checked', 'true');
            else
                subCheckBoxes.attr('checked', '');
        })   
    }
}


function initAdvancedSearch_OLD() {
    var menu_ul_all = $('ul.search_result li > ul');
    $(menu_ul_all).hide();

    $('#ctl00_ContentPlaceHolder2_ctl00_chkAll').click(function () {
        var $checkboxes = $('.search_result').find(':checkbox');

        if ($(this).is(':checked')) {
            $checkboxes.attr('checked', 'true');
        }
        else {
            $checkboxes.attr('checked', '');
        }
    });

    $('.search_hdg_L1 :checkbox').click(function () {
        var $checkboxes = $(this).parent().parent().parent().find('ul.search_result_L2 li :checkbox');
        if ($(this).is(':checked')) {
            $checkboxes.attr('checked', 'true');
        }
        else {
            $checkboxes.attr('checked', '');
        }
    });

    $('.search_hdg_L2 :checkbox').click(function () {
        var $checkparent = $(this).parents().find('.search_hdg_L1 :checkbox').filter(':first');

        var $checkboxes = $(this).parent().parent().parent().find('ul.search_result_L3 li :checkbox');
        if ($(this).is(':checked')) {
            $checkparent.attr('checked', 'true');
            $checkboxes.attr('checked', 'true');
        }
        else {
            $checkboxes.attr('checked', '');
        }
    });

    $('.search_result_L3 :checkbox').click(function () {
        var $checkparent = $(this).parents().find('.search_hdg_L2 :checkbox').filter(':first');
        var $checkparentparent = $checkparent.parents().find('.search_hdg_L1 :checkbox').filter(':first');

        if ($(this).is(':checked')) {
            $checkparentparent.attr('checked', 'true');
            $checkparent.attr('checked', 'true');
        }
    });

    //sub1 items
    $('ul.search_result li').each(function () {
        if ($(this).children("ul").length > 0) {
            $('.search_hdg_L1 span.more', this).addClass('menu_off');
        }
        else {
            $('.search_hdg_L1 span.more', this).removeClass('menu_off');
        }
    });

    //sub 2 items
    $('ul.search_result>li>ul>li').each(function () {
        if ($(this).children("ul").length > 0) {
            $('.search_hdg_L2 span.more', this).addClass('menu_off');
        }
        else {
            $('.search_hdg_L2 span.more', this).removeClass('menu_off');
        }

    });

    $('.search_hdg_L1 span.more').bind("click", function () {

        var toggleClass = $(this).attr("class");

        if (toggleClass == 'more menu_on') {
            $(this).attr('class', 'more menu_off').parent('div').siblings('ul').fadeOut('fast');
        }
        else if (toggleClass == 'more menu_off') {
            $(this).attr('class', 'more menu_on').parent('div').siblings('ul').fadeIn('fast');
        }
    });

    $('.search_hdg_L2 span.more').bind("click", function () {
        var toggleClass = $(this).attr("class");

        if (toggleClass == 'more menu_on') {
            $(this).attr('class', 'more menu_off').parent('div').siblings('ul').fadeOut('fast');
        }
        else if (toggleClass == 'more menu_off') {
            $(this).attr('class', 'more menu_on').parent('div').siblings('ul').fadeIn('fast');
        }

    });
}
