﻿var img = new Image();
var currPageImg;

$('#mainImg').css('display', 'none');

function loadImg() {
    img.src = currPageImg;
    if (img.width == 0 || img.height == 0) {
        setTimeout("loadImg()", 1000);
    }
    else {
        $('#mainImg').css('background-image', "url('" + img.src + "')");
        $('#mainImg').width(img.width);
        $('#mainImg').height(img.height);

        $('#mainImg').css('display', 'none');
        setTimeout("$('#mainImg').fadeIn(1000);", 500);
    }
}


function setMenuColorBack() {
    $(".poolsNav").children().each(function () {
        if ($(this).children("div").css("width") == "0px") {
            $(this).children("a").css("color", "#FFF");
            //alert($(this).children("div").css("width"));
        }
        else {
            $(this).children("a").css("color", "#000");
        }
    }
    )

    $('.poolsNav a[href$="' + pageName + '"]').css('color', '#000');
}

function onBefore() {
    if (this.alt != "" && this.alt != null) {
        $('#dynImgText').html(this.alt);
        $('#dynImgText').show();
        if ($(this).attr('pdf') != "" && $(this).attr('pdf') != null) {
            $('#dynPdfLink').html('<a href="/dynimages/t_gallery_imgs/files/' + $(this).attr('pdf') + '" target="_blank">קטלוג</a>');
            $('#dynPdfLink').show();
        }
        else {
            $('#dynPdfLink').hide();
        }
    }
    else {
        $('#dynImgText').hide();
        $('#dynPdfLink').hide();
    }
}

$(document).ready(function () {


    /* $('img[@src$=.png]').ifixpng(); */

    $("a").click(function (event) {
        var url = $(this).attr("href");
        if (url.substring(0, 7) != "http://") {
            event.preventDefault();
            $('#logo').animate(
            { "left": "+=200px" }, "slow", function () { window.location = url; });
        }
    });

    $(".poolsNav li").mouseenter(function () {
        if ($(this).children("div").width() == 0) {
            $(this).children("div").animate({ 'width': '103px' }, 250);
            $(this).css('opacity', 1);
            $(this).children("a").css("color", "#000");
        }
    });

    $(".poolsNav li").mouseleave(function () {
        linkObj = $(this).children("a");
        $(this).css('opacity', 0.7);
        $('.poolsNav a[href$="' + pageName + '"]').parent().css('opacity', '1');
        $(this).children("div").animate({ 'width': '0px' }, 250, function () { setMenuColorBack() });
    });

    $("#btn_next_pool_img").mouseenter(function () {
        $(this).animate({ width: '30px', height: '40px', opacity: '0.9', top: '290px' }, 250);
        $(this).children("img").animate({ top: '10px', right: '4px' }, 250);
    });

    $("#btn_next_pool_img").mouseleave(function () {
        $(this).animate({ width: '50px', height: '60px', opacity: '0.5', top: '280px' }, 250);
        $(this).children("img").animate({ top: '20px', right: '13px' }, 250);
    });


    $("#btn_prev_pool_img").mouseenter(function () {
        $(this).animate({ width: '30px', height: '40px', opacity: '0.9', top: '290px', right: '537px' }, 250);
        $(this).children("img").animate({ top: '10px', left: '4px' }, 250);
    });

    $("#btn_prev_pool_img").mouseleave(function () {
        $(this).animate({ width: '50px', height: '60px', opacity: '0.5', top: '280px', right: '517px' }, 250);
        $(this).children("img").animate({ top: '20px', left: '15px' }, 250);
    });

    $("#btn_next_pool_img").css('opacity', '0.5');
    $("#btn_prev_pool_img").css('opacity', '0.5');

    if ($('#tips_div').length > 0) {
        $('#tips_div').cycle({
            containerResize: 0,
            fastOnEvent: 0,
            timeout: 5000,
            delay: -5000,
            startingSlide: $('#mainImgClip img').size() - 1
        });
    }


    if ($('#tips_div').length > 0 || (noGallNavigation != null && noGallNavigation == true))  {
        $('#mainImgClip').cycle({
            containerResize: 0,
            fastOnEvent: 0,
            timeout: 5000,
            delay: -5000,
            startingSlide: $('#mainImgClip img').size() - 1,
            before: onBefore
        });

        $("#btn_prev_pool_img").hide();
        $("#btn_next_pool_img").hide();
        $("#btn_prev_pool_txt").hide();
        $("#btn_next_pool_txt").hide();
    }
    else {
        $('#mainImgClip').cycle({
            containerResize: 0,
            fastOnEvent: 0,
            timeout: 10000,
            delay: -10000,
            next: '#btn_prev_pool_img',
            prev: '#btn_next_pool_img',
            startingSlide: $('#mainImgClip img').size() - 1,
            before: onBefore
        });
    }

    setTimeout("$('.mainImgClipClass').css('display', 'block');", 200);
    $('.mainImgClipClass img').css({ opacity: 0 });
    setTimeout("$('.mainImgClipClass img').show('slow')", 500);


    
    dispLogo();

    selectFromMainMenu(pageName);

    /* run only on pools page */
    if ($('.poolsNav').length > 0) {
        $('.poolsNav li').css({ 'opacity': 0.7 });

        $('.poolsNav a[href$="' + pageName + '"]').css('color', '#000');
        //$('.poolsNav a[href$="' + pageName + '"]').css('background', 'url(images/mbtn_current.jpg) repeat-x');
        $('.poolsNav a[href$="' + pageName + '"]').css('background-color', '#f6f3ee');
        $('.poolsNav a[href$="' + pageName + '"]').parent().css('opacity', '1');
        $('.poolsNav a[href$="' + pageName + '"]').css('font-weight', 'bold');

        var currPoolIndex = $('.poolsNav a[href$="' + pageName + '"]').index('.poolsNav a');

        $('.poolsNav a').each(function (i) {
            /* $(this).text($(this).text()); */
            $(this).css({ display: 'inline-block' });
        });

        setTimeout("setPoolsScroler(" + currPoolIndex + ")", 400);



        /* Set current in bottom (numbers) menu */
        $('.poolsBottomNav a[href$="' + pageName + '"]').css('background', '#FFF');
        $('.poolsBottomNav a[href$="' + pageName + '"]').css('border-top', '1px solid #000');
        $('.poolsBottomNav a[href$="' + pageName + '"]').css('border-bottom', '1px solid #000');

        $(".grouped_elements").fancybox({
            'cyclic': true,
            'titleShow': true,
            'titlePosition': 'inside'
        });
    }
    else {
        loadImg();
    }

    $('.productImg').load(function () {
        $('.productImg').resize({
            maxWidth: 83,
            maxHeight: 83
        });
    }).each(function () {
        if (this.complete) $(this).trigger("load");
    });

    setTimeout("$('.crams').css('position', 'relative');", 1000);
})

function setPoolsScroler(currPoolIndex) {
    $('.poolsNav').css({ visibility: 'visible' });
    $('.poolsNav').css({ display: 'none' });
    $('.poolsNav').fadeIn(800);
}

function selectFromMainMenu(currPageName) {
    $('#secondMenu a[href$="' + currPageName + '"]').css('color', '#707070');
    /* $('#secondMenu a[href$="' + currPageName + '"]').css('font-weight', 'bold'); */
}

function dispLogo() {
    setTimeout("$('#logo').animate({ 'left': '-=200px'}, 'slow');", 500 );
}
