$(document).ready(function() {
	reloadHot();				   
	var langId = $.cookie('langId');
	var styleId = $.cookie('style');
	
	if ( styleId != undefined ) {		
		changeStyle( styleId );
	}else {
		changeStyle('red');
	}
	
	changeLang( langId );
});

function closePopup() {
	$('#box-lyric-popup').fadeOut('slow');
};


function openViewLyric( idBox ) {
	
	var cssName = $('a#a-' + idBox ).attr('class');
	var myText = $('#lyric_' + idBox );
	var myTextShow = $('#lyric_click_' + idBox );
	
	if ( cssName == 'title-up' ) {
		$( 'a#a-' + idBox ).removeClass().addClass( 'title');
		myText.css( {
			'height' : '70px',
			'display' : 'block' 
		});
		myTextShow.css('display' , 'none');
	}
	
	if ( cssName == 'title' ) {
		$( 'a#a-' + idBox ).removeClass().addClass( 'title-up');
		myText.css( 'display', 'none');
		myTextShow.css ({
			'display' : 'block',
			'height'  : 'auto'
		});
	}
};


// Thay doi style
function changeStyle( styleId ) {	
	var styleOld = $.cookie('style');
	
	$('a#' + styleOld ).css( 'border', '1px solid #FFF' );	
	
	// Set Cookie
	$.cookie( 'style', null); 
	$.cookie( 'style', styleId, { expires: 30, path: '/'}); 
	
	// Current
	$('a#' + styleId ).css( 'border', '1px solid #666' );
	changeCssFile( styleId );	
	
};

function changeCssFile( styleId ) {
	var css = document.getElementById ('css');
	var pathCss = base_url + 'css/';	
	css.href = pathCss + styleId + '.css';	
};

// Thay doi kieu go
function changeLang( LangId ) {
	
	if ( LangId == null ) {
		LangId = 'en';
	}
	
	if ( LangId == 'vi' ) {		
		$('#english').css( 'display', 'none');							 
		$('#vietnamese').css( 'display', 'block');
		setMethod(1); 
	}else {
		$('#vietnamese').css( 'display', 'none');							 
		$('#english').css( 'display', 'block');		
		setMethod(-1); 
	}
	
	$.cookie( 'langId', null);
	$.cookie( 'langId', LangId, { expires: 30,path: '/'} );
};

/**
 * Jquery Cookies
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function hideNews( id ) {
	$('#news-' + id + '-Moveable').css({ 'height' : '24px' });
};

function ajaxBox( id  ) {
	
	var text_values = $('#combo-' + id ).val();
	
	//alert( text_values );
	
	if ( text_values == 0 ) {
		hideNews( id );		
	}else {
		
		if ( text_values == 12 ) {
			set_height = 366;
		}else {
			set_height = 235;
		}
		
		$('#news-' + id + '-Moveable').css({ 'height' : set_height + 'px' });
		$('#box-' + id ).load( base_url + 'news/ajaxnew/' + id + '/' + text_values );
	}
	
};

function ajaxBox2( ) {
	var text_values = $('#kh-show').val();
	
	if( text_values == 0 ) {
		$('#kh-news-bottom').css({ 'height' : '24px' });
	}else {
	
		if( text_values == 5 ) {
			set_height = 127;
			set_height2 = 189;
		}
		
		if( text_values == 10 ) {
			set_height = 261;
			set_height2 = 313;
		}
		
		$('#kh-news-bottom').css({ 'height' : set_height2 + 'px' });
		$('#kh-50331648').css({ 'height' : set_height + 'px' });
		$('#kh-100663296').css({ 'height' : set_height + 'px' });
		$('#kh-268435456').css({ 'height' : set_height + 'px' });
	}
};


function ltrim(str){ 	
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
};
function rtrim_t(str) {
	if(str!=null){
		for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
			return str.substring(0,j+1);
	}else return "";
};


/**
Ham thuc hien viecj replace query
**/
function ProcessQuery(strings){
	var str = "";
	 str = strings.replace(/-/g,' ');
	 str = str.replace(/'/g,'');
	 str = str.replace(/"/g,'');
	 return str;
};

function getRandom() {
	$('#box-news-hot').fadeOut('slow');
	$('#box-news-hot').load( base_url + 'news/ajaxhot', '', reloadHot);
};

function reloadHot() {
	var times = 120000;
	$('#box-news-hot').fadeIn('slow');
	setTimeout( 'getRandom()' , times );
};


