(function() {
    var changingInterval = 8;
    var lastActive = 1;
    clicked = false;
    if_clicked = false;
    $(document).ready(function() {



        show_popup();
        initMenu();
        initSlider();
        initContmenu();
    });
    function show_popup() {
//        $('a.login-link').click(function() {
//            $('#popuplogin').toggle();
//            $('a.login-link').toggleClass('active');
//            //alert("open");
//            return false;
//        });
//        $('#popuplogin a.close').click(function() {

//            $('#popuplogin').hide();
//            $('a.login-link').removeClass('active');
//        });

        //  Pops the menu docked in upper right
        //        $('a.news-link').click(function() {
        //            if ($.browser.opera)
        //                $('#popup-news').css('left', '45px');
        //            else if ($.browser.safari)
        //                $('#popup-news').css('left', '23px');
        //            else $('#popup-news').css('left', '122px');
        //            clicked = true;
        //            $('#popup-news').css('display', 'block');
        //        });
        //        $('#popup-news a.close').click(function() {
        //            $('#popup-news').hide();
        //            clicked = false;
        //            $('a.news-link').parent().removeClass('active');
        //            $('a.news-link').parent().removeClass('hover');
        //        });
    };
    function initContmenu() {
        $('ul.solutions li img').css('opacity', 1);
        $('ul.solutions li.active img').css('opacity', 1);
        $('ul.solutions li').hover(
		function() {
		    $(this).find('img').css('opacity', 1);
		},
		function() {
		    $(this).find('img').css('opacity', 1);
		    $('ul.solutions li.active img').css('opacity', 1);
		}
	);
    }



    function initMenu() {
        var rel = "";



        $('ul.market > li').hover(
			function() {

			    rel = $(this).find('a').attr('rel');
			    $('#pic1').attr('src', rel); //set background pic to pic associated with market

			    clearInterval($timerHandler);
			    changingInterval = 300;
			    //clear the last active element during slideshow
			    var element = $($('ul.market > li').get(lastActive));
			    element.attr('class', '');



			    if ($.browser.msie && $.browser.version == '6.0' && $(this).hasClass('first')) {
			        if (!if_clicked)
			            $(this).css('padding-bottom', '20px');
			    }
			    //$('ul.market > li').not('.hover').addClass('not-hovered');
			}, //over
			function() {
			    initSlider();

			    if (!if_clicked)
			        $('ul.market > li').not('.hover').removeClass('not-hovered');
			    if ($.browser.msie && $.browser.version == '6.0' && $(this).hasClass('first')) {
			        if (!if_clicked)
			            $(this).css('padding-bottom', '0');

			    }
			} //out
		);

        //-------------------------------------------------------------------------------------------------

        $('#footer ul > li').hover(
			function() {
			    $(this).addClass('hover');
			}, //over
			function() {

			    if ($(this).find('a.login-link').size() == 0) {
			        $(this).removeClass('hover');
			    }
			    else {
			        if ($('#popuplogin').css('display') == 'none') {
			            $(this).removeClass('hover');
			        }
			    }
			} //out
		);

        $('#header ul.menu > li').hover(
			function() {
			    if (($(this).find('a.news-link').size()) > 0) {
			        if (!clicked) {
			            $('div#popup-news').css('left', '0');
			            $('div#popup-news').css('display', 'block');
			        }

			    }

			    if (($(this).find('a.prod-link').size()) > 0) {
			        if (!clicked) {
			            $('div#prodLineDiv').css('left', '0');
			            $('div#prodLineDiv').css('display', 'block');
			        }

			    }
			    
			    
			    
			    
			    
			    
			}, //over
			function() {
			    if (!clicked)
			        $('div#popup-news').css('display', 'none');
			   
			        
			} //out
		);


    };

    //This Function controls the picture rotation/ Button Highlight on the homepage
    function initSlider() {
        var rel = "";
        $('mainpicdiv').attr('src', "");

        var element;
        var i = 0;
        var j = 6; //Change this number to correspond to the number of markets

        $timerHandler = setInterval(function() {

            if (i == j) {
                i = 0;
                clearInterval($timerHandler);
                changingInterval = 300;
                var element = $($('ul.market > li').get(j-1));
                element.attr('class', '');

                rel = "images/img4.jpg";
                var mainPic = document.getElementById('pic1');
                if (mainPic != null) {
                    //fade pictures
                    $('#pic1').ImageSwitch({ Type: "FadeIn", NewImage: rel, EffectOriginal: false, Speed: 3000 });
                  
                }
                
                
                return;
            }
            rel = $($('ul.market > li > a').get(i)).attr('rel');


            var element = $($('ul.market > li').get(i));
            if (i == 0)
                var prevElement = $($('ul.market > li').get(j - 1));
            else
                var prevElement = $($('ul.market > li').get(i - 1));


            i++;
            prevElement.attr('class', ''); // remove previous div active
            element.attr('class', 'active'); //set div active
            var mainPic = document.getElementById('pic1');
            //alert(mainPic);
            if (mainPic != null) {
                //fade pictures
                $('#pic1').ImageSwitch({ Type: "FadeIn", NewImage: rel, EffectOriginal: false, Speed: 3000 });
                lastActive = i - 1;
            }
        }, changingInterval * 1000);
    };
})();





function expand(obj1, obj2, obj3) {

    //removes active styling from any div
    //that may be active in the cycling process


    //  clearInterval($timerHandler);

    $(obj2).stop().animate(
        {

            width: '653px', height: obj3
        }
        ,
        {
            queue: false, duration: 600 //, easing: 'easeInExpo'

        })

    //        for (var i = 0; i < 10; i++) {
    //            var element = $($('ul.market > li').get(i));
    //            element.attr('class', '');
    //        }



}


function contract(obj1, obj2, obj3) {


    $(obj2).stop().animate({ width: '0px', height: '0px' }, { queue: false, duration: 600//, easing: 'easeInExpo' 
    })


}


function changeActive(element, rowNum, elementBase) {
    var eachElement = elementBase;

    for (var i = 1; i <= rowNum; i++) {
        var anElement = elementBase + i.toString();
        document.getElementById(anElement).style.backgroundColor = "";
    }

    document.getElementById(element).style.backgroundColor = "#fff";

}

function highlight(element) {


    document.getElementById(element).style.backgroundColor = "White";


}

function removehighlight(element) {


    document.getElementById(element).style.backgroundColor = "";


}
