(function($) {
	

	
	
	$.fn.ellipsis = function(enableUpdating){
		var s = document.documentElement.style;
		if (!('textOverflow' in s || 'OTextOverflow' in s)) {
			return this.each(function(){
				var el = $(this);
				if(el.css("overflow") == "hidden"){
					var originalText = el.html();
					var w = el.width();
					
					var t = $(this.cloneNode(true)).css('position', 'absolute').hide()
					.css('width', 'auto').css('overflow', 'visible');
					el.after(t);
					
					var text = originalText;
					while(text.length > 0 && t.width() > el.width()){
						text = text.substr(0, text.length - 1);
						t.html(text + "...");
					}
					el.html(t.html());
					
					t.remove();
					
					if(enableUpdating == true){
						var oldW = el.width();
						setInterval(function(){
							if(el.width() != oldW){
								oldW = el.width();
								el.html(originalText);
								el.ellipsis();
							}
						}, 200);
					}
				}
			});
		} else return this;
	};
})(jQuery);


function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}


/*  Menu */
var currentItm;
function showSubmenu(){
	var snv = $(this).parent().find(".subnav");
	currentItm = this;	
	if(snv){
		jQuery.each($(".visiblesubnav"), function() {
				var csnv = $(currentItm).parent().find(".subnav");		
				if(csnv && csnv.length > 0 && $(this).attr("id") != csnv.attr("id")){
					$(this).slideUp('slow');
					$(this).removeClass("visiblesubnav");
					$(this).removeClass("active");
				}
			});
		snv.slideDown('fast').show();
		snv.addClass("visiblesubnav");
	}
}

function hideSubmenu(){
	var snv = $(this).parent().find(".subnav");
	if(snv){
		if(!snv.hasClass("active")){
			snv.slideUp('slow');
			snv.removeClass("active");
			snv.removeClass("visiblesubnav");
		}
	}
}

function overSubmenu(){
	$(this).addClass("active");
}

function outSubmenu(){
	$(this).removeClass("active");
	$(this).slideUp('slow');
	$(this).removeClass("visiblesubnav");
}

$(document).ready(function(){
	
	// subscribe colorbox
	$('#SubscribeLink').colorbox({contentWidth: "780px", contentHeight: "460px", contentIframe:true});

	$('#ConfirmLink').colorbox({contentWidth: "780px", contentHeight: "460px", contentIframe:true});
	$('#ConfirmLink').trigger('click');

	
	$('ul.quick-subscribe input.subscribe').val("");
	
	// hide hint on focus input
	$('ul.quick-subscribe input.subscribe').focus(function(){
		$('.quick-subscribe-hint').hide();
		$('#SubscribeLink').trigger('click');
	});
	
	$('ul.quick-subscribe input.subscribe').blur(function(){
		if($(this).val() == ""){
			$('.quick-subscribe-hint').show();
		}
	});
	
	$("ul.quick-subscribe input.subscribe").keydown(function(e){
		if (e.keyCode == 13) {
			$('#SubscribeLink').trigger('click');
		}
	});
	
	//  popup subscribe if not shown before
	var showSubscribe = getCookie('subscribedlg');
	if( showSubscribe == null ){
		setCookie('subscribedlg', 1, 356);
		$('#SubscribeLink').trigger('click');
	}
	
	$('#Topslider').nivoSlider({
		directionNav:false,
        controlNav: false, 
        keyboardNav:false,
		pauseOnHover:false,
		pauseTime: 6000
	});
	
	var snid = 0;
	jQuery.each($(".subnav"), function() {
			$(this).attr("id", "sn" + snid);
			snid++;
			var lnk = $(this).parent().find("a");
			if(lnk[0]){
				$(lnk[0]).click(function() { showSubmenu(); return false; });
			}
		});
	$(".mainnav-item a").hoverIntent({
			sensitivity: 10, 
			interval: 400, 
			over: showSubmenu, 
			timeout: 1400, 
			out: hideSubmenu});
	$(".subnav").hoverIntent({
			sensitivity: 10,
			timeout: 1400,
			interval: 400, 
			over: overSubmenu, 
			out: outSubmenu});

	/* route */
	$(".routepopup").colorbox({ contentWidth: "780px", contentHeight: "460px", contentIframe:true});
	
	// overflow elipses
	$(".newsitem h2").ellipsis(true);
			
});



