$(document).ready(function(){

//cycle images in news page
//        $('#banner-news')
//        .livequery(function(){
//            $(this).cycle('fade');
//        });

//halaka! client area menu.
//        $('#halaka').livequery('click', function(){
//            $('.ikawka').remove();
//            $('.blank').toggleClass('blank1');
//        });

//logs the download
        $('.download').click(function(){
            var item = $(this).attr('rel');
	     var _url = 'admin/clientarea/logdownload/'+ item;
	     var loc = $(this).parent('a').attr('href');

            //jQuery.get('admin/clientarea/logdownload/'+ item);

		$.ajax({
		type: 'GET',
              url: _url,
              success: function(msg){
			if(msg){
				window.location = loc;
			}
			return false;
                 }

       	});
            return false
        });

//liveperson tooltip

//watermark
        $("#insic").Watermark("Site Search");
        $("#ca-username")
            .livequery(function(){
                $(this).Watermark("Username");
            });

        $("#ca-password")
            .livequery(function(){
                $(this).Watermark("********");
            });
//rounded corner

        $(".activesub")
        .livequery(function(){
            $(this).corner('5px');
        });

        $(".hassub").livequery('click', function(){
            $(this).children('span').toggleClass('active');
            $('ul.submodule').toggleClass('hidesub');

            return false;
        });

        $(".tmodule")
        .livequery(function(){
            $(this).corner('10px');
        });

        $('#menu li a.training').click(function(){
                window.location.href = $(this).attr(href);
         });

        $('#menu li a#halaka').click(function(){
                window.location.href = $(this).attr(href);
         });

        $('#trainmenu li a').click(function(){
                window.location.href = $(this).attr(href);
         });
//        $(".homepage-gt")
//        .livequery(function(){
//            $(this).corner('10px');
//        });
        $(".mc-curve")
        .livequery(function(){
            $(this).corner('10px');
        });

//accordion

        $(".accordion h5:first")
        .livequery(function(){
            $(this).addClass("active");
        });

        $(".accordion div.accordionchild:first")
        .livequery(function(){
            $(this).show();
        });

        $(".accordion h5")
        .livequery('click', function(){
                $(this).next("div.accordionchild").slideToggle("fast")
                .siblings("div:visible").slideUp("fast");
                $(this).toggleClass("active");
                $(this).siblings("h5").removeClass("active");
        });

//accordion2 archive

        $(".accordion2 h5:first")
        .livequery(function(){
            $(this).addClass("active");
        });

        $(".accordion2 h5")
        .livequery('click', function(){
                $('span.loading').remove();
                $(this).before('<span class="loading">&nbsp;</span>');
                $(this).next("div.accordionchild")
                .load('home/getarchivecontent/'+ $(this).attr('id')).slideToggle("fast")
                .siblings("div:visible").slideUp("fast");

                $(this).toggleClass("active");

                $(this).siblings("h5").removeClass("active");

               $('span.loading').fadeOut('slow');

        });


//flag tooltip
        $(".language a").hover(function() {
            $(this).next("em").animate({opacity: "show", top: "15"}, "slow");
        }, function() {
            $(this).next("em").animate({opacity: "hide", top: "25"}, "fast");
        });

// ajax page transition

	var hash = window.location.hash.substr(1);
	var href = $('#menu li a').each(function(){
		var href = $(this).attr('href');
		if(hash==href.substr(0,href.length)){
			var toLoad = hash+' #content';
                        $('#menu').removeClass('training');
			$('#content').load(toLoad)
            $('.news-box').remove()
		}
	});

//******************************************************************************
//Using livequery in submenu transition.
//******************************************************************************
$('#submenu li a').livequery('click', function(){

	var toLoad = $(this).attr('href')+' #content';
        var curPage = $(this).attr('href').substr(5, $(this).attr('href').length);

        $('#menu').removeClass('training');

        $('#load').remove();

		$('#content').fadeOut('slow',loadContent);

		$('#wrapper').append('<span id="load">LOADING...</span>');

        $('#load').fadeIn('slow');



          if($.browser.msie){

            var test = $(this).attr('href').replace(/^.*page/, '#page');

            window.location.hash = test;

          }else{

            window.location.hash = $(this).attr('href');

          }




        function loadContent() {

            //remove the content completely resolved the transition bug.
            $('#contents').remove();

            //load the content
			$('#content').load(toLoad,'',showNewContent());

		}

        function hideLoader() {

			$('#load').fadeOut('slow');

		}

		function showNewContent() {

			$('#content').fadeIn('slow',hideLoader());

		}
		return false;

	});

//******************************************************************************

	$('#menu li a').click(function(){

	var toLoad = $(this).attr('href')+' #content';
        var curPage = $(this).attr('href').substr(11, $(this).attr('href').length);
        var curPageMenu = $(this).attr('href').substr(5, $(this).attr('href').length);
        var activePage = $(this).attr('class');

        $('#menu').removeClass();

        $('#menu').addClass(activePage+' menu');

        $('#menuWrap').removeClass();

        $('#load').remove();

		$('#content').fadeOut('slow',loadContent);

        $('#menuWrap').addClass(curPageMenu+'Wrap');

		$('.news-box').remove();

        $('.breadcrumbs ul').append(getBreadCrumbs());

		$('#wrapper').append('<span id="load">LOADING...</span>');

        $('#load').fadeIn('slow');

		window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length);


        function getBreadCrumbs() {
            $('.breadcrumbs ul').load('breadcrumbs/index/'+curPage,'');
        }

		function loadContent() {

            //remove the content completely resolved the transition bug.
            $('#contents').remove();

            //load the content
			$('#content').load(toLoad,'',showNewContent());

		}

        function hideLoader() {

			$('#load').fadeOut('slow');

		}

		function showNewContent() {

			$('#content').fadeIn('slow',hideLoader());

		}

        function loadAccordion() {

            $.getScript('static/js/accordion.js');

        }

		return false;

	});

});
