var order = 'all';
var proc = 'page';

function viewTutorial(info){
    document.getElementById('tutorial').style.display = 'none';
    if(info == ""){
        return;
    }   
    var infoArr = info.split('|');
    if (infoArr.length < 3 || infoArr[1] == "") {
        return;
    }
    document.getElementById('phoneName').innerHTML = infoArr[0];
    document.getElementById('tutorialDetail').src = infoArr[1];
	if (infoArr[2] == 'true') {
		var detailPhoneName = 'samsung-' + infoArr[0].toLowerCase().replace('samsung', '').replace(/^\s\s*/, '').replace(/\s\s*$/, '').replace(' ', '-').replace('sgh-', '').replace('sch-', '').replace('gt-', '').replace('sph-','');
		document.getElementById('pinfoBtn').innerHTML = '<a href="' + urlRoot + 'telefone-celular/' + detailPhoneName + '" title="Product Info"><img src="' + urlRoot + 'images/common/btn/bt_pinfo_off.gif" alt="Product Info" onmouseout="ImageOut(this)" onmouseover="ImageOver(this)" /></a>';
	} else {
		document.getElementById('pinfoBtn').innerHTML = '';
	}
    document.getElementById('tutorial').style.display = 'block';
}

function sort(orderBy) {
    proc = 'sort';
    var paramMap = new Object();
    paramMap['siteCd'] = document.getElementById('siteCd').value;
    paramMap['langCd'] = document.getElementById('langCd').value;
    paramMap['order'] = orderBy;
    SupportTutorialsServiceImpl.getTutorialList(paramMap, callBackTutorialList);
    order = orderBy;
    var innerHTML = "";
    if (order == 'latest') {
        innerHTML += '<p class="on">Latest</p>';
        innerHTML += '<p class="link"><a href="javascript:sort(\'all\');" title="Alphabetic">Alphabetic</a></p>';
    } else {
        innerHTML += '<p class="link"><a href="javascript:sort(\'latest\');" title="Latest">Latest</a></p>';
        innerHTML += '<p class="on">Alphabetic</p>';        
    }
    document.getElementById('sort').innerHTML = innerHTML;                       
    document.getElementById('startRow').value = 1;
    document.getElementById('endRow').value = 7;    
}

function getTutorialList(tran) {
    proc = 'page';
    var paramMap = new Object();
    var startRow = parseInt(document.getElementById('startRow').value);
    var endRow = parseInt(document.getElementById('endRow').value);
    var totalRow = parseInt(document.getElementById('totalRow').value);
    var siteCd = document.getElementById('siteCd').value;
    var langCd = document.getElementById('langCd').value;
    
    if (tran == 'L') {
        if (startRow > 1) {
            startRow = startRow - 1;
            endRow = startRow + 6;
            document.getElementById('startRow').value = startRow;
            document.getElementById('endRow').value = endRow;
            paramMap['startRow'] = startRow;
            paramMap['endRow'] = endRow;
            paramMap['siteCd'] = siteCd;
            paramMap['langCd'] = langCd;
            paramMap['order'] = order;
            SupportTutorialsServiceImpl.getTutorialList(paramMap, callBackTutorialList);
            
        }
    } else if(tran == 'R') {
        if (endRow < totalRow) {
            endRow = endRow + 1;
            startRow = endRow - 6;
            document.getElementById('endRow').value = endRow;
            document.getElementById('startRow').value = startRow;
            paramMap['startRow'] = startRow;
            paramMap['endRow'] = endRow;
            paramMap['siteCd'] = siteCd;
            paramMap['langCd'] = langCd;
            paramMap['order'] = order;
            SupportTutorialsServiceImpl.getTutorialList(paramMap, callBackTutorialList);
                    
        }
    }
}

function callBackTutorialList(tutorialList) {
    
    if (proc == 'sort') {
        var obj = document.getElementById('tutorialSelect');
        obj.options.length = 0;
        obj.options[0] = new Option('Select your Mobile Phone Model', '');
        if(tutorialList.length > 0){
            for (i = 0; i < tutorialList.length; i++) {
                obj.options[i + 1] = new Option(nullToStr(tutorialList[i].miniName), nullToStr(nullToStr(tutorialList[i].phoneName) + '|' + tutorialList[i].flashFileName) + '|' + tutorialList[i].hasDetail);
            }
        }       
    }
    
    var innerHTML = '';
    var startRow = parseInt(document.getElementById('startRow').value);
    var endRow = parseInt(document.getElementById('endRow').value);
    var totalRow = parseInt(document.getElementById('totalRow').value);
    if(tutorialList.length > 0){
        if (startRow == 1) {
            innerHTML += '<li class="bt_prv"><img src="' + urlRoot + 'images/common/btn/bt_supp_prv_dim.gif" alt="first" /></li>';
        } else {
            innerHTML += '<li class="bt_prv"><a href="javascript:getTutorialList(\'L\');" title="previous"><img src="' + urlRoot + 'images/common/btn/bt_supp_prv_off.gif" alt="previous" onmouseout="ImageOut(this)" onmouseover="ImageOver(this)" /></a></li>';
        }
        
        for(i = 0; i < tutorialList.length; i++){
            if (i == 7) break;
            innerHTML += '<li><p class="img" title="' + nullToStr(tutorialList[i].phoneName) + '"><a href="javascript:viewTutorial(\'' + nullToStr(nullToStr(tutorialList[i].phoneName) + '|' + tutorialList[i].flashFileName) + '|' + tutorialList[i].hasDetail + '\')" title="' + nullToStr(tutorialList[i].phoneName) + '">' + '<img src="' + phoneImagePath + nullToStr(tutorialList[i].phoneImage) + '" onerror="this.src=\'' + urlRoot + 'images/common/noimg/noimg_60.gif\'" alt=""></a></p><p class="txt" title="' + nullToStr(tutorialList[i].phoneName) + '"><a href="javascript:viewTutorial(\'' + nullToStr(tutorialList[i].phoneName) + '|' + (tutorialList[i].flashFileName) + '|' + tutorialList[i].hasDetail + '\')" title="' + nullToStr(tutorialList[i].phoneName) + '">' + nullToStr(tutorialList[i].miniName) + '</a></p></li>';
        }
        if (endRow < totalRow) {
            innerHTML += '<li class="bt_nxt"><a href="javascript:getTutorialList(\'R\');" title="next"><img src="' + urlRoot + 'images/common/btn/bt_supp_next_off.gif" alt="next" onmouseout="ImageOut(this)" onmouseover="ImageOver(this)" /></a></li>';           
        } else {
            innerHTML +='<li class="bt_nxt"><img src="' + urlRoot + 'images/common/btn/bt_supp_next_dim.gif" alt="last" /></li>';
        }
        
    } else {
        innerHTML += '<li></li>';
    }
    document.getElementById('pd_list').innerHTML = innerHTML;   
}

function nullToStr(arg) {
    return arg == null ? '' : arg;
}
