$(document).ready(function () {
    checkSize();
    checkSizeInterval = setInterval(function () {
        checkSize();
    }, 500);
    if ($('.accordion').length) {
        initAccordion();
    }
    initSmallSlideShow();
    initBigSlideShow();
    tabsInit('blockCarroselTabs');
    /**/
    var url = window.location.href.split('#');
    if (url[1] != undefined && url[1] != null) {
        $().find('*[name=' + url[1] + ']').each(function (index) {
            $(this).parent().addClass('glossaryHilight');
        });
    }
	
	//IF RECYCLE AUTO COMPLETE EXISTS
	var elRecycleAuto = $('#txtAutoComp');
	if(elRecycleAuto.length){
		initRecycleAuto();
	}

});

//FUNCTION TO INIT AUTOCOMPLETE FOR RECYCLE

var initRecycleAuto = function () {
    var elBtnAuto = $('.colSubmitAuto input'),
	elInputAuto = $("#txtAutoComp"),
	sAutoValue = null,
	sRedirectUrl = null,
	elLabelAutoComp = $('.labelAutoComp'),
	sWsUrl = $("#wsAutoComp").html();

    //INPUT FOCUS BLUR ADD REMOVE LABEL
    elInputAuto.focus(function () {
        if ($(this).val() == '') {
            elLabelAutoComp.addClass('labelHidden');
        }
    });
    elInputAuto.blur(function () {
        if ($(this).val() == "") {
            elLabelAutoComp.removeClass('labelHidden');
        }
    });
    //BTN SUBMIT AUTO COMPLETE
    elBtnAuto.click(function (e) {
        var sInputAutoVal = elInputAuto.val();
        if (sAutoValue != null && sAutoValue == sInputAutoVal) {
            window.location = sRedirectUrl;
        }
        e.preventDefault();
        e.stopPropagation();
    });

    //var availableTags = ["ActionScript", "ActionScript2", "ActionScript3", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme"];

    //INIT INPUT AUTOCOMPLETE
    elInputAuto.autocomplete({
        source: sWsUrl,
        minLength: 3,
        select: function (event, ui) {
            if (ui.item) {
                sAutoValue = ui.item.value;
                sRedirectUrl = ui.item.redirectUrl;
            } else {
                sAutoValue = null;
            }
        }
    });
}

function checkSize() {
    var centralHeight = $('.centralContainer').height();
    var sideHeight = $('.sideContainer').height();
    if (sideHeight > centralHeight + 100) {
        $('.centralContainer').css({ 'height': (sideHeight - 93) + 'px' });
    }
}

function initSmallSlideShow() { 
    $(".paging").show();
    $(".paging a:first").addClass("active");
    var myDescription = $('.image_reel a:eq(0) img').attr('alt');
    var myLink = $('.image_reel a:eq(0)').attr('href');
    $('.pictureDescription span').html(myDescription);
    var imageWidth = $(".window").width();
    var imageSum = $(".image_reel img").size();
    var imageReelWidth = imageWidth * imageSum;
    $(".image_reel").css({ 'width': imageReelWidth });
    rotate = function () {
        var triggerID = $active.attr("rel") - 1;
        var image_reelPosition = triggerID * imageWidth;
        $(".paging a").removeClass('active');
        $active.addClass('active');
        $(".image_reel").animate({
            left: -image_reelPosition
        }, 500);
    };

    rotateSwitch = function () {
        play = setInterval(function () {
            $active = $('.paging a.active').next();
            var linkIndex = $active.prevAll().length;
            var myDescription = $('.image_reel a:eq(' + linkIndex + ') img').attr('alt');
            var myLink = $('.image_reel a:eq(' + linkIndex + ')').attr('href');
            $('.pictureDescription span').html(myDescription);
            $('.pictureDescription a').attr('href', myLink);
            if ($active.length === 0) {
                $active = $('.paging a:first');
            }
            rotate();
        }, 3000);
    };

    //rotateSwitch();

    /*
    $(".image_reel a").hover(function () {
        clearInterval(play);
    }, function () {
        rotateSwitch();
    });
    */

    $(".paging a").click(function () {
        $active = $(this);
        var linkIndex = $active.prevAll().length;
        var myDescription = $('.image_reel a:eq(' + linkIndex + ') img').attr('alt');
        var myLink = $('.image_reel a:eq(' + linkIndex + ')').attr('href');
        $('.pictureDescription span').html(myDescription);
        $('.pictureDescription a').attr('href', myLink);
        //clearInterval(play);
        rotate();
        //rotateSwitch();
        return false;
    });
}


function initBigSlideShow() {
    $(".bigPaging").show();
    $(".bigPaging a:first").addClass("active");
    var myDescription = $('.bigImage_reel a:eq(0) img').attr('alt');
    var myLink = $('.bigImage_reel a:eq(0)').attr('href');
    $('.bigPictureDescription span').html(myDescription);
    var imageWidth = $(".bigWindow").width();
    var imageSum = $(".bigImage_reel img").size();
    var imageReelWidth = imageWidth * imageSum;
    $(".bigImage_reel").css({ 'width': imageReelWidth });

    bigRotate = function () {
        var triggerID = $active.attr("rel") - 1;
        var image_reelPosition = triggerID * imageWidth;
        $(".bigPaging a").removeClass('active');
        $active.addClass('active');
        $(".bigImage_reel").animate({
            left: -image_reelPosition
        }, 500);

    };

    bigRotateSwitch = function () {
        bigPlay = setInterval(function () {
            $active = $('.bigPaging a.active').next();
            var linkIndex = $active.prevAll().length;
            var myDescription = $('.bigImage_reel a:eq(' + linkIndex + ') img').attr('alt');
            var myLink = $('.bigImage_reel a:eq(' + linkIndex + ')').attr('href');
            $('.bigPictureDescription span').html(myDescription);
            $('.bigPictureDescription a').attr('href', myLink);

            if ($active.length === 0) {
                $active = $('.bigPaging a:first');
            }
            bigRotate();
        }, 5000);
    };

    //bigRotateSwitch();

    /*
    $(".bigImage_reel a").hover(function () {
        clearInterval(bigPlay);
    }, function () {
        bigRotateSwitch();
    });
    */

    $(".bigPaging a").click(function () {
        $active = $(this);
        var linkIndex = $active.prevAll().length;
        var myDescription = $('.bigImage_reel a:eq(' + linkIndex + ') img').attr('alt');
        var myLink = $('.bigImage_reel a:eq(' + linkIndex + ')').attr('href');
        $('.bigPictureDescription span').html(myDescription);
        $('.bigPictureDescription a').attr('href', myLink);
        //clearInterval(bigPlay);
        bigRotate();
        //bigRotateSwitch();
        return false;
    });


}

function tabsInit(e) {
    var tabContainers = $('div.' + e + ' > div ');
    tabContainers.hide().filter(':first').show();
    $('div.' + e + ' ul.tabNavigation a').click(function () {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('div.' + e + ' ul.tabNavigation li').removeClass('selected');
        $(this).parent().addClass('selected');
        return false;
    }).filter(':first').click();
}

function initAccordion() {
    $('.accordionNode .accordionBlock').hide();
    $('.accordion').each(function () {
        var accContainer = $(this);
        var accTab = accContainer.find('.accordionNode > a')
        accTab.click(function () {
            var content = $(this).parent();
            if ($(this).next('.accordionBlock').is('.open')) {
                $(this).next('.accordionBlock').slideUp('fast').removeClass('open');
                $(this).removeClass('active');
            } else {
                $(this).addClass('active');
                $(this).next('.accordionBlock').slideDown('fast', function () { }).addClass('open');
                $(this).siblings().removeClass('active');
                $(this).siblings().next('.accordionBlock').slideUp('fast').removeClass('open');
                $('.areaContentContainer').find('.centralContainer').attr('style', '');
            }
            return false;
        })
    });
}



