$(document).ready(function() {
	reloadHot();				   
	var langId = $.cookie('langId');
	var styleId = $.cookie('style');
	
	if ( styleId != undefined ) {		
		changeStyle( styleId );
	}else {
		changeStyle('blue');
	}
	
	changeLang( langId );
	
	
	$('.box-video-playlist ul li').hover(
		function() {
			$(this).addClass('current2').css( 'cursor', 'pointer' );									  
		}, function() {
			$(this).removeClass('current2');	
		}
	);

	$('#mp3').click(
		  	function(){
				$(this).addClass('current');
				$('#img').removeClass('current');
				$('#web').removeClass('current');
				//document.boxsearch.action=base_url + "mp3/search/#play_song/1/0/";
		});
});

function closePopup() {
	$('#box-lyric-popup').fadeOut('slow');
	
};

function showPopup( id ) {	 
	var el = '#id-' + id;
	var lyric = $(el).html();
	var id2 = $(el) .attr("rel");
	/*var url = 'http://123.30.6.9:3760/GetItem.aspx?id=' + id2 + '&callback=?';
	$.getJSON(url,function(data){
	  if(data.ly){
	    $('#lyric-view').html( lyric  + data.ly );
	  }
	});*/
	link = base_url + "mp3/getly/";
	/*alert(id2);
	alert(link);
	$.ajax({
	  url: link,
	  cache: false,
	  success: function(html){
	   // $("#results").append(html);
	    $('#lyric-view').html( lyric  + html );
	  }
	});*/
	
	 $.ajax({
	   type: "POST",
	   url: link,
	   data: "id="+id2,
	   success: function(msg){
	      $('#lyric-view').html( lyric  + msg  );
	   }
	 });


	$('#box-lyric-popup').fadeIn('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 );	
	
	// Thay doi logo
	$('#logo').attr({
		src: base_url + 'public/img/' + styleId + '.png',
		//src: base_url + 'public/logo/logo_trungthu2 copy.jpg',
        title: 'NaisCorp.com',
		alt: 'NaisCorp.com',
		height: '128', //160 //128
		width: '272' //310 //272
		//height: '173', //160 //128
		//width: '300' //310 //272
	});
	
};

function changeCssFile( styleId ) {
	var css = document.getElementById ('css');
	var pathCss = base_url + 'public/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: '/'} );
};

function showBox( id_box ) {
	
	if ( id_box == 1 ) {		
		$('#hidden-send').toggle('slow');
	}	
		
	if ( id_box == 2 ) {	
		$('#hidden-comment').toggle('slow');
	}			
};

/**
 * 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 replacequery(str) {	
	str = ltrim(str);	
	var str2 = str.indexOf('.');
	if ( str2 == 0 ) { 
		str = str.replace('.', '');
	}
	str = str.replace(/ /g,'-');
	str = str.replace(/\%20/g,'-');
	str = str.replace(/\?/g,'');
	str = str.replace("\|",'');
	str = str.replace(/\//g,'');
	str = str.replace(/\\/g,'');
	return str
}

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 "";
};
function trim(str) {
	return ltrim(rtrim_t(str));
};
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
};
/**
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 );
};

// type of dom element being moved
var elementType = 'div';
// the end text of the moveable element ids
var elementIdentifier = 'Moveable';
// the end text of the move up link ids
var upLinkIdentifier = 'MoveUp';
// the end text of the move down link ids
var downLinkIdentifier = 'MoveDown';

function move_div(elementId, direction){
	var i;
	var set = $(elementType + '[id$=' + elementIdentifier + ']');
	var total_div = set.length;
	var clone = '';
		
	for( i=0; i < total_div; i++ ) {
		
		if(set.get(i).id == elementId ) {
			
			if(direction == 'down' && i == (total_div - 1)){
				// can�t move down
				break;
			}else if(direction == 'up' && i == 0){
				// can�t move up
				break;
			}
			// clone the item
			clone = $('#' + elementId).clone();
			
			// remove it
			$('#' + elementId).remove();

			if(direction == 'down'){
				// add the clone below the next item
				clone.insertAfter(set.get(i+1));
				
			}else if(direction == 'up'){
				// add the clone before the previous item
				clone.insertBefore(set.get(i-1));
				
			}
			
			//break;
		}
		
		
	}
	 
	// rework the links
	moveable();	
	
}

/**
* Set links to allow divs to be moveable
*/
function moveable(){
	var set;

	// down links should all be shown except the last one
	set = $('a[id$=' + downLinkIdentifier + ']');
	set.show();
	//$('#' + set.get(set.length-1).id).hide();
	
	// set the click function to move down
	set.click( function() {
		move_div($('#' + this.id).parent().get(0).id, 'down');
	});

	// up links should all be shown except the first one
	set = $('a[id$=' + upLinkIdentifier + ']');
	set.show();
	//$('#' + set.get(0).id).hide();		

	// set the click function to move up
	set.click( function() {
		move_div($('#' + this.id).parent().get(0).id, 'up');	
	});
}

$(function() {
	moveable();
})


function search_news( id_textbox, url_base ) {
	var ob 		= document.getElementById( id_textbox );
	var decode 	= replacequery_search( trim(ob.value ));
	//var decode 	= ob.value;
	
	url_temp = url_base + 'news/search/0/'+ decode ;
	
	if( decode != null && decode != '' )
		window.location.href = url_temp;
	
}
function replacequery_search(str) {	
	//alert(str);
	str = trim(str);
	str 	= encodeURIComponent(str);
	//alert(str);
	str = ltrim(str);	
	var str2 = str.indexOf('.');
	if ( str2 == 0 ) { 
		str = str.replace('.', '');
	}
	//str = str.replace(/\+/g,'%2B');
	str = str.replace(/ /g,'+');
	str = str.replace(/\%20/g,'+');
	//str = str.replace(/\?/g,'%3F');
	//str = str.replace("\|",'%7C');
	//str = str.replace(/\//g,'%2F');
	str = str.replace(/\\/g,'%5C');
	return str
}
function replacequery_test(str) {	

	//str = str.replace(/\+/g,'%2B');
	str = str.replace(/\+/g,' ');
	return str
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
