
$('.news-headline-box').css({'visibility':'hidden'});

$(document).ready(function(){
    // init menu
	if (typeof(qm_create) == "function" && typeof(sd)!="undefined") {
		qm_create(0, false, 0, 500, false, false, false, false, false);
	}
	
	// News headlines preview  
    var nh_enebled = true;
    var nh_newsBg = '/img/basic_template/sipka.png';
	var nh_mover = 1;
	var nh_visibleCount = 3;
	var nh_count = $('.news-headline-ul > li').size();

	$('.news-headline-box').css({'visibility':'visible'});
	$('.news-headline-up,.news-headline-down').hide();
    
    if (nh_enebled) {
		if (nh_count > nh_visibleCount) {
			$('.news-headline-up', '.news-headline-down').show();
		}

	    function nhTogle(x, src){
	        $('#new-head-img-view').css('background-image', 'url(' + src + ')');
	        $('.news-headline-ul > li').css('background-image', 'none');
	        $(x).css('background-image', 'url(' + nh_newsBg + ')');
	        $('.news-headline-li-hover-color').removeClass('news-headline-li-hover-color');
	        $(x).addClass('news-headline-li-hover-color');
	        $(x).find('a').addClass('news-headline-li-hover-color');
	        $(x).find('p').addClass('news-headline-li-hover-color');
	    }
		
		function moveNews(dir){
			if (nh_count > nh_visibleCount) {
				if (dir == 'up') {
					$('.news-headline-ul li:nth-child(' + nh_mover + ')').hide();
					$('.news-headline-ul li:nth-child(' + (nh_mover + nh_visibleCount) + ')').fadeIn();
					nh_mover += 1;
				}
				if (dir == 'down') {
					$('.news-headline-ul li:nth-child(' + nh_mover + ')').fadeIn();
					$('.news-headline-ul li:nth-child(' + (nh_mover + nh_visibleCount) + ')').hide();
					nh_mover -= 1;
				}
				
				if (nh_mover > nh_count - nh_visibleCount - 1) {
					nh_mover = nh_count - nh_visibleCount - 1;
				}
				if (nh_mover < 1) {
					nh_mover = 1;
				}
			}
        }
		
        $('.news-headline-content').append('<div id="new-head-img-view" class="rounded"></div>');
        $('.news-headline-ul > li').each(function(i, x){
            var src = $(x).find('img').attr('src');
            var url = $(x).find('a').attr('href')
            
            $(x).bind('click', function(){
                window.location.href = url;
            });
            $('#new-head-img-view').bind('click', function(){
                window.location.href = url;
            });
            $(x).bind('mouseover', function(){
                nhTogle(x, src);
            });
            if (i == 0) {
                nhTogle(x, src);
            }
            if (i > 2) {
                $(x).css('display', 'none');
            }
            $('.news-headline-item-img').hide();
        });
        
        $('.news-headline-up').bind('click mouseover', function(m){
            moveNews('up');
        });
		 $('.news-headline-down').bind('click mouseover', function(m){
            moveNews('down');
        });
    }
	
	// init shadowbox
    // http://www.shadowbox-js.com/options.html
	if (typeof(Shadowbox) == "object"){
	    Shadowbox.init({
	        handleOversize: "drag"
	    });
	}
});

// flash msg popup
$('#flashMessage').ready(function(){
	var f = $('#flashMessage');
	if (f.length>0) {
		var winW = $(document).width();
		var winH = $(document).height();
		$('body').prepend('<div class="popupWindow"></div>');
		$('.popupWindow').width(winW).height(winH).fadeTo("slow", 0.7).click(function(x){
			hideAll(x);
		});
		f.addClass('flashPopup').prependTo('body').width(winW - 80).click(function(x){
			hideAll(x);
		});
		function hideAll(x){
			$('#flashMessage').fadeOut("slow", function(){
				$(this).hide();
			});
			$('.popupWindow').fadeOut("slow", function(){
				$(this).hide();
			});
		}
	}
});

