﻿// JScript File

function ProductsListingFithHeight(){
    
    var maxHeight=0;
    
    var oThumbCells = $('.ProductsListingThumbnailCell')
    oThumbCells.each(function(idx,oItem){
        if(maxHeight<$(oItem).height()) maxHeight=$(oItem).height()
    })    
    oThumbCells.each(function(idx,oItem){
        $(oItem).height(maxHeight)
    })
    
    maxHeight=0;
    var oInfoCells = $('.ProductsListingProductInfo')
    oInfoCells.each(function(idx,oItem){
        if(maxHeight<$(oItem).height()) maxHeight=$(oItem).height()
    })
    oInfoCells.each(function(idx,oItem){
        $(oItem).height(maxHeight)
    })    
}

function ShowAlbum(sAlbumId){
    //Tracking
    var sAction = "Albums.aspx"
    var sData = 'trackalbumid=' + sAlbumId
        
        $.ajax({
				method:'get', 
				url:sAction, 
				data:sData,
				cache: false,
				beforeSend:function(){				    
				},
				complete:function(){					
				},
				success:function(sHTML){                    
				}
        })
    //Show album
    $('#bmvAlbum' + sAlbumId + '').mbGallery({maskBgnd:'#666', maskOpacity:.8});
}

////////////////////////////////////////////////////////
//Survey
////////////////////////////////////////////////////////
function SubmitSurvey(ModuleSurveyID, sAction, NoOptionMessage, ThankMessage){
	var sSurveyId = '#BmvSurvey' + ModuleSurveyID
	var oCheckedOptions=$(sSurveyId + ' input:checked')
	
	if(! oCheckedOptions.length){
	    if(jQuery.trim(NoOptionMessage)!='') alert(NoOptionMessage)
		return false
	}
	
	var sIds = oCheckedOptions.serialize()
	while(sIds.indexOf(oCheckedOptions[0].name + '=')>=0){
		sIds=sIds.replace(oCheckedOptions[0].name + '=','')
	}
	while(sIds.indexOf('&')>=0){
		sIds=sIds.replace('&',',')
	}
	
	$.ajax({
		method		:'get',
		url			:sAction,
		data		:'ModuleSurveyID=' + ModuleSurveyID + '&option=' + sIds,
		beforeSend	:function(){			
		},
		complete	:function(){
		},
		success		:function(sResult){
			var oResultContainer = $(sSurveyId + ' .Result-Container')			
			oResultContainer.html(sResult)
			ShowResult(ModuleSurveyID)
			if(jQuery.trim(ThankMessage)!='') alert(ThankMessage)
		}
	})
}

function ShowSurvey(ModuleSurveyID){
	var sSurveyId = '#BmvSurvey' + ModuleSurveyID
	var btnResult = $(sSurveyId + ' .bmv-Suervey-result-button')
	var oOptionContainer = $(sSurveyId + ' .Option-Container')
	var oResultContainer = $(sSurveyId + ' .Result-Container')
	oResultContainer.css('display','none')
	oOptionContainer.css('display','block')
	btnResult.show()
}

function ShowResult(ModuleSurveyID){
	var sSurveyId = '#BmvSurvey' + ModuleSurveyID
	var btnResult = $(sSurveyId + ' .bmv-Suervey-result-button')
	var oOptionContainer = $('#BmvSurvey' + ModuleSurveyID + ' .Option-Container')
	var oResultContainer = $('#BmvSurvey' + ModuleSurveyID + ' .Result-Container')
	oResultContainer.css('display','block')
	oOptionContainer.css('display','none')
	btnResult.hide()
}
	
function CreateSurvey(ModuleSurveyID,sAction,NoOptionMessage,ThankMessage){    
	var sSurveyId = '#BmvSurvey' + ModuleSurveyID
	var oSurvey = $(sSurveyId)
	if(oSurvey){
		var oOptions = oSurvey.find(':input')
		oOptions.attr('name','BmvSurveyOption' + ModuleSurveyID)
		
		var btnSubmit = $(sSurveyId + ' .bmv-Suervey-submit-button')
		var btnResult = $(sSurveyId + ' .bmv-Suervey-result-button')
		
		var oOptionContainer = $(sSurveyId + ' .Option-Container')
		var oResultContainer = $(sSurveyId + ' .Result-Container')
	
		//Setup submit button		
		if(btnSubmit){
			btnSubmit.click(function(){
				var bShowingResult = (oResultContainer.css("display")=='block')
				if(bShowingResult){
					ShowSurvey(ModuleSurveyID)
				}
				else{
					SubmitSurvey(ModuleSurveyID,sAction,NoOptionMessage,ThankMessage)
				}
				
				return false
			})
		}
		
		//Setup result button
		if(btnResult){
			btnResult.click(function(){
				ShowResult(ModuleSurveyID)				
				return false
				
			})
		}
	
	}	
}
////////////////////////////////////////////////////////
//End of Survey
////////////////////////////////////////////////////////



////////////////////////////////////////////////////////
//Right tab
////////////////////////////////////////////////////////

function CreateRightTab(){
	var oTabLeft=$('#tdLeftTab')
	var oTabRight=$('#tdRightTab')
	
	var oTabLeftContainer=$('#Right_tab_left_item_container')
	var oTabRightContainer=$('#Right_tab_right_item_container')
	
	if(oTabLeft.length && oTabRight.length && oTabLeftContainer.length && oTabRightContainer.length){
		
		oTabRightContainer.hide()
		
		oTabLeft.click(function(){
			if(oTabLeft.attr('class')=='Right_tab_left_item_normal'){
				oTabLeft.attr('class','Right_tab_left_item_selected')
				oTabRight.attr('class','Right_tab_right_item_normal')
				
				//Show tab contant
				oTabRightContainer.hide()
				oTabLeftContainer.show()
				
			}
		})
		
		oTabRight.click(function(){
			if(oTabRight.attr('class')=='Right_tab_right_item_normal'){
				oTabRight.attr('class','Right_tab_right_item_selected')
				oTabLeft.attr('class','Right_tab_left_item_normal')
				
				//Show tab contant
				oTabLeftContainer.hide()
				oTabRightContainer.show()
			}
		})
		
	}
	
}
////////////////////////////////////////////////////////
//End of right tab
////////////////////////////////////////////////////////

$(document).ready(function(){   
    
    InitThirthLevelBox()
    
    CreateRightTab();
    
    $('a img').attr('border','0')
        
    //Home page tab	    
    if(($(".Carousel").length>0)&&($(".PhotoCarousel").length>0)){
        $(function() {
            $(".Carousel").jCarouselLite({
                btnNext: ".next",
                btnPrev: ".prev",
		        visible:6,
		        scroll:6,
		        speed:900
            });
        });
        $(function() {
            $(".PhotoCarousel").jCarouselLite({
                btnNext: ".nextAlbum",
                btnPrev: ".prevAlbum",
		        visible:6,
		        scroll:6,
		        speed:900
            });
        });
        $('#divPhotosTab').hide()
    }
     
})

function clientActiveTabChanged(sender, args) {
    if(sender.get_activeTabIndex()==0){ //Video tab
        $('#divPhotosTab').hide()
        $('#divVideosTab').show()        
    }
    else{   //Photo gallery tab
        $('#divVideosTab').hide()
        $('#divPhotosTab').show()
    }
}

////////////////////////////////////////////////////////////////////////////////
// VERTICAL NAVIGATOR
////////////////////////////////////////////////////////////////////////////////

function TogleThirthLevelBox(oActive,idBox){
	
	if($(oActive).attr('href')=='#'){
		$('.V-nav-level3-box:not(#' + idBox + ')').hide()
		$('#' + idBox + ':hidden').show('fast')
		$('.V-nav-item-selected-level2').removeClass('V-nav-item-selected-level2').addClass('V-nav-item-level2')
		$(oActive).parent().addClass('V-nav-item-selected-level2')
	}	

	return false
}

function InitThirthLevelBox(){	
	$('.V-nav-level3-box').hide()
	$('.V-nav-level3-box-selected').show()
	$('#divVerticalNavigator').css('display','')
}



function CrateTab(sContainerSelector){

	function SetDoubleTabIndex(sContainerSelector,iTab){
		if(iTab<2){
			$(sContainerSelector+ ' #Tab2').attr('class','Tab_item_normal')
			$(sContainerSelector+ ' #TabContent2').hide()
			
			$(sContainerSelector+ ' #Tab1').attr('class','Tab_item_selected')
			$(sContainerSelector+ ' #TabContent1').show()
		}
		else{
			$(sContainerSelector+ ' #Tab1').attr('class','Tab_item_normal')
			$(sContainerSelector+ ' #TabContent1').hide()
			
			$(sContainerSelector+ ' #Tab2').attr('class','Tab_item_selected')
			$(sContainerSelector+ ' #TabContent2').show()			
		}
		$.cookie(sContainerSelector, iTab)
	}
	
	$(sContainerSelector+ ' #Tab1').hover(
		function(){
			$(sContainerSelector+ ' #Tab1').addClass('Tab_item_hover')
		},
		function(){
			$(sContainerSelector+ ' #Tab1').removeClass('Tab_item_hover')
		}
	).click(function(){
		SetDoubleTabIndex(sContainerSelector,1)
	})
	
	
	$(sContainerSelector+ ' #Tab2').hover(
		function(){
			$(sContainerSelector+ ' #Tab2').addClass('Tab_item_hover')
		},
		function(){
			$(sContainerSelector+ ' #Tab2').removeClass('Tab_item_hover')
		}
	).click(function(){
		SetDoubleTabIndex(sContainerSelector,2)
	})
	
	var iSelectedTab = $.cookie(sContainerSelector)
	if(iSelectedTab !='2') iSelectedTab=1
	
	SetDoubleTabIndex(sContainerSelector,iSelectedTab)	
}

$(document).ready(function(){
	CrateTab('#Double_tab1')
	CrateTab('#Double_featured_tab')
})




// ******************************************************
// Favourite Stories functions
// ******************************************************

function addFavouriteStory(storyId, title, link) {
	deleteFavouriteStory(storyId);
	current = getCookie("favourite_Stories");
	if (current == null)
		current = "";
	if (current.length > 0)
		current = "!" + current;
	current = storyId + "|" + title + "|http://" + document.location.host + link + current;
	setStaticCookie("favourite_Stories", current);
}

function deleteFavouriteStory(storyId) {
	current = getCookie("favourite_Stories");
	if (current == null)
		return;

	stories = current.split("!");
	newStories = "";	
	for (i=0; i<stories.length; i++) {
		story = stories[i].split("|");
		if (story[0] != storyId)
			newStories += "!"+stories[i];
	}
	if (newStories.length > 0)
		newStories = newStories.substring(1);
	setStaticCookie("favourite_Stories", newStories);
}

function clearAllFavouriteStories() {
	setStaticCookie("favourite_Stories", "");
}

function displayFavouriteStories() {
	// o^ng hardcode da^'y :p so chua :| he he

	current = getCookie("favourite_Stories");
	if (current == null) {
		document.write("Chua có tin nào trong danh sách");
		return;
	}
	document.write("<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">");
	stories = current.split("!");
	for (i=0; i<stories.length; i++) {
		story = stories[i].split("|");
		document.write("<tr>");
		document.write("<td>");
		document.write("<a href=\"#\" onclick=\"javascript:window.opener.location='"+story[2]+"';\">"+story[1]+"</a>");
		document.write("</td>");
		document.write("<td>");
		document.write("<input type=\"image\" src=\"/images/Icons/Icon_Del.gif\" onclick=\"javascript:deleteFavouriteStory('"+story[0]+"');window.location.reload();\">");
		document.write("</td>");
		document.write("</tr>");
	}
	document.write("</table>");
}

function clearPersonalInfo() {
	deleteStaticCookie("favourite_Stories");
}

function FeaturedArticlesSpander(oArticleBox){  //Article box's id
    var oBox = $(oArticleBox)
    var bShowing = false
    if(oBox.get(0).visible==true) bShowing=true
    
    if(bShowing){
        oBox.hide('fast')
    }
    else{        
        oBox.show('fast')
        oBox.css('display','')
    }
    oBox.get(0).visible = !bShowing
    return false
}

