﻿// JavaScript Document
function changetab(obj){
   	$('.vj_process_news > a').removeClass('active_news');
	$('.vj_border_news > ul').css('display','none');
	$('#'+obj).addClass('active_news');
	$('#'+obj.replace('tab','cont')).css('display','block');
	
}

function showHideIdAuto_headertop() {
    if (document.getElementById('vj_ingfo_f_login').style.display ==''){
        document.getElementById('vj_ingfo_f_login').style.display = 'none';
	}
    else{ 
        document.getElementById('vj_ingfo_f_login').style.display = '';
	}

    return;
}

var curentContent="contsk1";
var int;
function tommi_active_tab(obj, num){

		var curentTab=obj.id;
		var curentContent_1=curentTab.replace("tabsk","contsk");
		switch(num){
			case 0:
				curentContent=curentTab.replace("tabsk","contsk");
				var tabs=$('#' + curentTab).parent().children();
				var contents=$('#' + curentContent).parent().children();
				window.clearInterval(int)
				int=setInterval("loadPage()",5000);	
				break;
		}
		
		$(tabs).each( function(){								
				$(this).attr('class','');
		});
		$(contents).each( function(){								
				$(this).hide();
		});
		
		$('#' + curentTab).addClass('active_tab');
		switch(num){
			case 0:
				$('#' + curentContent).fadeIn('slow');	
				break;
		}		 	
	}
	
	
var status=0;
function change_sukien(obj, box){
	closeAll(box);	
	var content =obj.parentNode.getElementsByTagName("div")[0];
	if(!status){
		content.style.display="none";
		status=1;
		}
	else {
		obj.parentNode.className="ac_sukien_js";
		content.style.display="block";
		status=0;
		}
}

function closeAll(box){
	var box =document.getElementById(box);
	for(var i=0 ; i<box.childNodes.length; i++){
		if((box.childNodes[i].nodeType!=3) && (box.childNodes[i].nodeType!=8)){
			box.childNodes[i].className="";
			box.childNodes[i].getElementsByTagName("div")[0].style.display="none";
		}
	}
	status=1;
}	



function TNCom_getDocumentSize(val)
{
	var xScroll,yScroll,value;
	
	if(window.innerHeight&&window.scrollMaxY)
	{
		xScroll=window.innerWidth+window.scrollMaxX;
		yScroll=window.innerHeight+window.scrollMaxY;
	}
	else if(document.body.scrollHeight>document.body.offsetHeight)
	{
		xScroll=document.body.scrollWidth;
		yScroll=document.body.scrollHeight;
	}
	else{
		xScroll=document.body.offsetWidth;
		yScroll=document.body.offsetHeight;
	}
	
	var windowWidth,windowHeight;
	
	if(self.innerHeight)
	{
		if(document.documentElement.clientWidth){
			windowWidth=document.documentElement.clientWidth;
		}
		else{
			windowWidth=self.innerWidth;
		}
		windowHeight=self.innerHeight;
	}
	else if(document.documentElement&&document.documentElement.clientHeight)
	{
		windowWidth=document.documentElement.clientWidth;
		windowHeight=document.documentElement.clientHeight;
	}
	else if(document.body)
	{
		windowWidth=document.body.clientWidth;
		windowHeight=document.body.clientHeight;
	}
	
	if(yScroll<windowHeight){
		pageHeight=windowHeight;
	}
	else{
		pageHeight=yScroll
	}
	
	if(xScroll<windowWidth){
		pageWidth=xScroll
	}
	else{
		pageWidth=windowWidth
	}
	
	arrayPageSize=new Array(pageWidth,pageHeight,windowWidth,windowHeight)	
	return arrayPageSize[val]
}// end function

function jb_active_tab(obj){
        
        var curentTab=obj;
        var curentContent=curentTab.replace("tab","cont");
		
		var tabs = $('#' + curentTab).parent().children().removeClass('active');
		var contents=$('#' + curentContent).parent().children();
		
        $(tabs).each( function(){								
				if($(this).hasClass("active")) $(this).removeClass('active');		   
		});		
		
		$(contents).each( function(){								
				$(this).hide();
		});	
		
		$('#' + curentTab).addClass('active');
		$('#' + curentContent).fadeIn('slow');	
		var pos =0;
		
		switch (curentTab){
			case "tab1":
				pos='left top';
				break;
			case "tab2":
				pos='left center';
				break;
			case "tab3":
				pos='left bottom';
				break;
			}
		$('#'+curentTab).parent().css({'backgroundPosition':pos});

}
/*show picture*/
var t_id = setInterval(animate,20);
var pos=0;
var dir=2;
var len=0;
function animate(){
	var elem = document.getElementById('progress');
	if(elem != null){
		if (pos==0) len += dir;
		if (len>32 || pos>79) pos += dir;
		if (pos>79) len -= dir;
		if (pos>79 && len==0) pos=0;
		elem.style.left = pos;
		elem.style.width = len;
	}
}
function remove_loading(){
	this.clearInterval(t_id);
	var targelem = document.getElementById('loader_container');
	targelem.style.display='none';
	targelem.style.visibility='hidden';
	var t_id = setInterval(animate,60);
}

/*B - js main Menu **/
// Valid value for omi_activemenu
// menu_home,menu_member,menu_earn,menu_redeem,menu_coupon,menu_community,menu_account,menu_help

var menu_timer=0;
function setActiveMenu(menu)
{
    //setlink for top menu -iehack
    $('#menu_home > span').click(function(){document.location.href='/';});
    $('#menu_member > span').click(function(){document.location.href='/dang-nhap';});
    $('#menu_account > span').click(function(){document.location.href='/tai-khoan';});
    $('#menu_earn > span').click(function(){document.location.href='/kiem-diem';});
    $('#menu_redeem > span').click(function(){document.location.href='/quy-doi';});
    $('#menu_coupon > span').click(function(){document.location.href='/phieu-giam-gia';});
    $('#menu_game > span').click(function(){document.location.href='/games';});
    $('#menu_community > span').click(function(){document.location.href='/goc-ichoice';});
    $('#menu_help > span').click(function(){document.location.href='/FAQ/tro-giup';})
    //alert(omi_activemenu);
	if (typeof omi_activemenu == 'undefined') 
		omi_activemenu = 'menu_home';
	if (typeof menu !='undefined')
		omi_activemenu = menu;	
		submenubar = '#sub'+omi_activemenu;
	$('#'+omi_activemenu).toggleClass('vj_active_menu');	
	$(submenubar).css('display','block');
	if (typeof omi_activesubmenu != 'undefined' && omi_activesubmenu !='')
	$('#'+omi_activesubmenu).css('font-weight','bold');
}
function restoreSubMenu(menu)
{   
	menu_timer = setTimeout('setSubMenu()',400);
	
}

function setSubMenu(submenubar)
{   if (omi_activemenu=='') omi_activemenu='menu_home';
	if (typeof submenubar =='undefined')
		submenubar = omi_activemenu;
	$('.omi_submenu > li').css('display','none');//change this line
	$('#sub'+submenubar).css('display','block');
}

function setProfileAnswer(biglist)
{
	
	jQuery.each(biglist.split(','),function(){
		sublist = this.split(':');
		switch (sublist[0])
		{
			case 'cbox': 
			case 'rdio': $(".answer>input[value="+sublist[1]+"]").attr('checked','checked'); break;
			case 'drpd': $(".answer>option[value='"+sublist[1]+"']").attr('selected','selected'); break;
		}
	});
}

function loadQuickSurvey()
{
	if ($('#quick_survey'))
    {
        $.get('/Earn/ajax_Quicksurvey.aspx',{action:"question",seed:Math.random()},function(data){
            $('#quick_survey').html(data);
       
        if (document.location.toString().indexOf('earn')>0) 
                if (typeof $('#btnSubmit').val()!='undefined')               
            $('#quick_survey').html('<p class="titleQuestion">Câu hỏi trong ngày:</p>'+$('#quick_survey').html());
       
        $('#btnSubmit').click(function(){qsSubmit();});
        $('#btnSkip').click(function(){qsSkip();});      
		});
	}
}
function qsSkip()
{
    var qid = $('.question').attr('qid');
    if (typeof qid=='undefined')
    {
        loadQuickSurvey();
    }
    else{
    $.get('/Earn/ajax_quicksurvey.aspx',
        {action:'skip',qid:qid},
        function(data){
        $('#quick_survey').html(data);
        
        if ($('#btnSubmit') && document.location.toString().indexOf('earn')>0)                
        $('#quick_survey').html('<p class="titleQuestion">Câu hỏi trong ngày:</p>'+$('#quick_survey').html());

        $('#btnSubmit').click(function(){qsSubmit();});
        $('#btnSkip').click(function(){qsSkip();});  
    });
    }
}

function qsSubmit()
{
    var s='';
    var qid = $('.question').attr('qid');
   $(".answer>input").each(function (i) {
       if ($(this).is(':checked'))
       {
            s += (s=='')?$(this).val():','+$(this).val();
       }
   });
   
    $.get('/Earn/ajax_quicksurvey.aspx',
        {action:'answer',qid:qid,aid:s},
    function(data){
    if (data.substring(0,4)=='ERR:')
    {
        $('#errorMsg').html('<div class="messagebox"><span><strong>Thông báo</strong><br/>'+data.substring(4)+'</span></div>');
    } else 
    {
        $('#quick_survey').html(data);
        //alert(data);
        
        if ($('#btnSubmit') && document.location.toString().indexOf('earn')>0)                
        $('#quick_survey').html('<p class="titleQuestion">Câu hỏi trong ngày:</p>'+$('#quick_survey').html());

        if ($('#btnSubmit'))
        {   
           $('#btnSubmit').click(function(){qsSubmit();});
        }
        $('#btnSkip').click(function(){qsSkip();}); 
    }
    
    });
}

function loadBanners()
{
	var tmp = [];
	var i=1;
	var j = 0;
 $('.adsbanner').each(function()
    { 
       var label = document.getElementById('hidden_userid_for_adsbanner') +'';
		tmp[i] = $(this); 
		$.getJSON("http://adscenter.ichoice.com.vn/Response.aspx?jsoncallback=?", 
			{	action:"banner",
				index:i,
				type:tmp[i].attr("type"),
				position:tmp[i].attr("position"),
				userid:label.value,
				url:encodeURI(document.location)},
			
			function(data){
				if (data.banner.index)
				{
				    tmp[data.banner.index].html(data.banner.html);
				    tmp[data.banner.index].find('code').each(function(){
				    eval($(this).html());});
				}}
		 );
		 i++;
	});
}
function loadForumNotice()
{
	if($('.navlinks:first'))
		$.get('/Resources/Content/forumnotice.html',{},function (data){ $('.navlinks:first').after(data);});
	
}

function omi_TopBannerRotate()
{

    $('#pause_banner').click(function() { $('#show_pic_banner').cycle('pause'); return false; }); 
    $('#play_banner').click(function() { $('#show_pic_banner').cycle('resume'); return false; }); 
    $('#show_pic_banner').before('<div id="nav_bannner">').cycle({
    fx: 'fade', delay: -500, speed: '2000', timeout: 5000, pause: 1, pager: '#nav_bannner',
    next: '#next_banner', prev: '#prev_banner' });

}

function omi_BannerRotate(bannername,max)
{
    //$('#pause_banner').click(function() { $('#show_pic_banner').cycle('pause'); return false; }); 
    //$('#play_banner').click(function() { $('#show_pic_banner').cycle('resume'); return false; }); 
	if (typeof max=='undefined') max=0;
	if (max>0)
	{
		while ($(bannername).children().length>max)
		{
			r = Math.floor(Math.random()*$(bannername).children().length);
			$(bannername).children().eq(r).remove();
		}
	}
	
    $(bannername).cycle({
    fx: 'fade', 
	delay: -500, 
	speed: 'slow', 
	timeout: 5000, 
	pause: 1, 
	pager: '#nav_mainbanner',
    next: '#next_banner', 
	prev: '#prev_banner',
	pagerEvent: 'mouseover',
	pauseOnPagerHover: true
	
	});

}

function omi_LeftBannerRotate()
{

    $('#pause_banner').click(function() { $('#show_pic_banner').cycle('pause'); return false; }); 
    $('#play_banner').click(function() { $('#show_pic_banner').cycle('resume'); return false; }); 
    $('#show_pic_banner').before('<div id="nav_bannner_small">').cycle({
    fx: 'fade', delay: -100, speed: '500', timeout: 3000, pause: 1, pager: '#nav_bannner_small',
    next: '#next_banner', prev: '#prev_banner' });

}

function getmultiads()
{
imglength = $('#multibanner>div').length;
 do {   index = Math.floor(Math.random() * imglength);      
 index1 = Math.floor(Math.random() * imglength);      
 index2 = Math.floor(Math.random() * imglength);  
 }while(index1 == index || index2 == index || index1 == index2)  
 
 $('#multibanner>div:eq('+index+')').show();
 $('#multibanner>div:eq('+index1+')').show();
 $('#multibanner>div:eq('+index2+')').show();
}

function clickOnAdsBanner(object)
{
    var label = document.getElementById('hidden_userid_for_adsbanner');
	$.getJSON("http://adscenter.ichoice.com.vn/AdsIntermedia.aspx?position="+object.getAttribute("position")+"&userid="+label.value+"&url="+document.location+"&jsoncallback=?");
   }

// Convert Array index to loop
// Ex: array has 3 items: a0,a1,a2; item a3 become  a0, a4 become a1...
// also work for negative value
  
function loopback(cursor,max)
{
	if (cursor<0) 
		return (cursor+max);
	if (cursor >= max) 
		return (cursor-max);	
	return cursor;	
}

var cSRS = 0;
function loadSRS(sindex)
{
	
	if (typeof sindex=="undefined")
	sindex = 0;
	cSRS  = sindex;	
	
	var sArray = $('#SRSData>li');
	cSRS = loopback(cSRS,sArray.length);
	var c0 = cSRS;
	var c1 = loopback(c0+1,sArray.length);
	var c2 = loopback(c0+2,sArray.length);
	var c3 = loopback(c0+3,sArray.length);
	
	$('#SRS0').html($(sArray[c0]).html());
	$('#SRS1').html($(sArray[c1]).html());
	$('#SRS2').html($(sArray[c2]).html());
	$('#SRS3').html($(sArray[c3]).html());

}

var ccoupon=0;
function loadCoupons(c)
{
	var cArray = $('#coupons>div');
	if (typeof c=="undefined")
		c = 0;
	ccoupon = loopback(c,cArray.length);
	cprev=loopback(ccoupon-1,cArray.length);
	cnext=loopback(ccoupon+1,cArray.length);

	$('.GalleryThumb>ul>#thumbleft').html($(cArray[cprev]).children('a').html());
	$('.GalleryThumb>ul>#thumbleft').click(function(){selectCoupon(cprev);});
	$('.GalleryThumb>ul>#thumbcenter').html($(cArray[ccoupon]).children('a').html());
	$('.GalleryThumb>ul>#thumbcenter').click(function(){selectCoupon(c);});
	$('.GalleryThumb>ul>#thumbright').html($(cArray[cnext]).children('a').html());
	$('.GalleryThumb>ul>#thumbright').click(function(){selectCoupon(cnext);});
}

var csuggestion=0;
function load_suggestions(c)
{
	var cArray = $('#suggestions>div');
	//alert(cArray.length);
	if (typeof c=="undefined")
		c = 0;
	for (i=0;i<6;i++)	
	{
	    ci = loopback(c+i,cArray.length);//alert($(cArray[ci]).html());
	    $('#p'+(i+1)).html($(cArray[ci]).html());
	}
	
	csuggestion=loopback(c,cArray.length);
}

function selectCoupon(cindex)
{
	var coupons  = $('#coupons>div');
	if (typeof cindex=="undefined") cindex = 0;
	$('.GalleryView').html($(coupons[cindex]).html());
	$('.GalleryView>a>img').css('width','250px').css('height','160px');
}

function promotion_khtt()
{
	var now =  new Date();
	if ($.cookie('khtt_201006')!= now.getDate())
	{
		var img = new Image();
		img.src = '/images/banner/khtt.jpg';
		$.cookie('khtt_201006',now.getDate(),{
			expires : 1,
			path    : '/',
			domain  : 'ichoice.com.vn'
		});	
		$.fancybox('<a href="http://events.ichoice.com.vn/khach-hang-than-thiet/" target="_blank" onclick="$.fancybox.close();" ><img border=0 src="/images/banner/khtt.jpg"></a>');
		
	}
}


$(document).ready(function()
{
    //[phuong]:delete cache for IE7
    $.ajaxSetup ({ cache: false});
    
	setActiveMenu();
    // run the code in the markup!
    $('div pre code').each(function() {
        eval($(this).text());
    });
	
	loadBanners();
	loadSRS(cSRS);
	loadQuickSurvey();
	//loadForumNotice();

	loadCoupons(ccoupon);
	selectCoupon(ccoupon);
	$('.Gallery>.BntRefer>.back').click(function(){loadCoupons(ccoupon-3);});
	$('.Gallery>.BntRefer>.next').click(function(){loadCoupons(ccoupon+3);});
	$('.AddpointRight>.BntRefer>.back').click(function(){loadSRS(cSRS-4);});
	$('.AddpointRight>.BntRefer>.next').click(function(){loadSRS(cSRS+4);});
	
	load_suggestions(csuggestion);
	$('.roll').click(function(){load_suggestions(csuggestion+1);});
    promotion_khtt();
	
});

$('#ctl00_ucMenu3_ucTop_LoginStatus1').click(function(){
$.cookie('GUID','',{expires : 1,	path    : '/',domain  : 'ichoice.com.vn'});	
});
