﻿///<reference path="/javascript/jquery-1.6.2-vsdoc.js"/>
selected_tab_index = 0;
country_list_start_index = 0;
fp_pic_container_id = '';
fp_title_id = '';
fp_intro_id = '';
fp_cnav_div_id = '';

function cdata(name, c_id, lang_id) { this.name = name; this.c_id = c_id, this.lang_id = lang_id; }

function InitCountryList(pic_container_id, title_id, intro_id, cnav_div_id) {
    fp_pic_container_id = pic_container_id;
    fp_title_id = title_id;
    fp_intro_id = intro_id;
    fp_cnav_div_id = cnav_div_id;

    if (countrylist.length > 3) {
        $('#c_move_tab_right').show();
    }
    /*$('#fp_intro_title').text(i_title);
    $('#fp_intro').text(i_intro);*/
}

function MoveCountryList(left) {
    var tab_container = $('#c_tab_container');
    var tab_count = tab_container.children().length;
    //var previous_selected_index = country_selected_index;
    var changed = false;

    if (left == false && tab_count > 0 && countrylist.length > tab_count) {
        ++country_list_start_index;
        changed = true;
    } else if (left == true && country_list_start_index > 0) {
        --country_list_start_index;
        changed = true;
    }

    if(changed)
    {
        for (var x = 0; x < tab_count; ++x) {
            var index = x + country_list_start_index;
            //alert(index);
            var tab_text = countrylist[index].name;
            //alert(tab_text);
            tab_container.children().eq(x).text(tab_text);
        }

        var new_selected_index = 0;
        if (left == false) {
            new_selected_index = selected_tab_index - 1;
            new_selected_index = new_selected_index < 0 ? 0 : new_selected_index;
        } else {
            new_selected_index = selected_tab_index + 1;
            new_selected_index = new_selected_index > tab_count - 1 ? tab_count - 1 : new_selected_index;
        }

        if (country_list_start_index + tab_count == countrylist.length) {
            $('#c_move_tab_right').hide();
        } else {
            $('#c_move_tab_right').show();
        }

        if (country_list_start_index > 0) {
            $('#c_move_tab_left').show();
        } else {
            $('#c_move_tab_left').hide();
        }

        ShowCountry(new_selected_index);


    }
}

function ShowCountry(tab_index) {
    
    $('#c_tab_container').children().each(function () {
        var id = $(this).attr('id');
        id = id.charAt(id.length - 1);
        var className = 'c_tab_' + id + '_selected';
        if ($(this).hasClass(className)) {
            $(this).removeClass(className);
        }
    });

    var tab_container = $('#c_tab_container');
    var pic_container = $('#' + fp_pic_container_id);
    var c_id = 0;
    var lang_id = 0;
    var cindex = tab_index + country_list_start_index;
    if (countrylist.length > tab_index) {
        c_id = countrylist[cindex].c_id;
        lang_id = countrylist[cindex].lang_id;
    }
    
    tab_container.children().eq(tab_index).addClass('c_tab_' + tab_index + '_selected');
    selected_tab_index = tab_index;
    
    var params = '{country_id: "' + c_id + '", language_id: "' + lang_id + '"}';
    $.ajax(
    {
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        url: '/services.asmx/GetCountryData',
        data: params,
        dataType: "json",
        success: function (data) {
            var obj = data.d;

            if (pic_container.children().length > 0) {
                pic_container.children().remove();
            }
            //alert(obj.img_url);
            pic_container.append('<img id="fp_b_pic" src="' + obj.img_url + '" />');
            $('#' + fp_title_id).text(obj.title_text);
            $('#' + fp_intro_id).text(obj.intro_text);
            $('#' + fp_cnav_div_id).attr('onclick', 'location.href=\'' + obj.c_url + '\';');
            $('#' + fp_cnav_div_id).find('a').each(function () {
                $(this).attr('href', obj.c_url);
            });
        }

    });
};

