$(document).ready(function() {
	
	// $('#tab_2').removeClass('tab_active').addClass('tab_inactive');
	
	var activeTab = 1;
	//console.log($('div.modulewrap .mid #subject_landing #tab_1').text());
	if($.trim($('div.modulewrap .mid #tab_1').text()) == ''){
        $('div.modulewrap .mid #tab_1').text('No Data Currently Available.');
        activeTab = 2;
    }
    if($.trim($('div.modulewrap .mid #tab_2').text()) == ''){
        $('div.modulewrap .mid #tab_2').text('No Data Currently Available.');
        activeTab = 1;
    }

    if(2 == activeTab){
        $('.tabnav ul li:first-child a').removeClass('active');
        $('.tabnav ul li:last-child a').addClass('active');
        $('div.modulewrap .mid #tab_1').removeClass('active').addClass('tab_inactive');
        // $('.tabnav ul li:last-child a').addClass('active');
        // $('div.modulewrap .mid #tab_2').removeClass('tab_inactive').addClass('tab_active');
    }
    else{
        $('.tabnav ul li:last-child a').removeClass('active');
        $('.tabnav ul li:first-child a').addClass('active');
        $('div.modulewrap .mid #tab_2').removeClass('tab_active').addClass('tab_inactive');
    }

	// if($.trim($('div.modulewrap .mid #tab_1').text()) == ''){
    //     // $('div.modulewrap .mid #tab_1').text('No Data Currently Available.');
	// 	// $('.tabnav ul li:first-child').hide();
	// 	$('.tabnav ul li:last-child a').addClass('active');
	// 	// $('div.modulewrap .mid #tab_1').removeClass('tab_active').addClass('tab_inactive');
	// 	$('div.modulewrap .mid #tab_2').removeClass('tab_inactive').addClass('tab_active');
	// }
    // else{
        
    // }
	
	if($('.offline_schools table tr').length == 1){
		$('.tabnav ul li:last-child').hide();
	}	
	
	

	// -----------------------
	// --- Selectification ---
	// -----------------------


    var defaults = {
        'best_cities': '-Select a City-',
        'nearby_cities': '-Select a City-',
        'schools_by_state': '-Select a State-',
        'nerby_states': '-Select a State-',
        'nearby_states_box': '-Select a State-',
        'nearby_box': '-Select a State-',
        'similar_areas': '-Select a Study-',
        'related_links': '-Select a Related Link-'
    };


function SortByinnerHTML(a, b){
  var aName = a.innerHTML.toLowerCase();
  var bName = b.innerHTML.toLowerCase(); 
  return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}


function SortByTextContent(a, b){
  var aName = $(a).text().toLowerCase();
  var bName = $(b).text().toLowerCase();
  return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}


    $('div.schools_in_state_box li').hide();

	$('ul.selectify').each(function(){
		var list=$(this);


		select=$(document.createElement('select')).insertBefore($(this).hide());
        if(list.hasClass('state-schools'))
            select.addClass('state-schools');
		select.on('change',function(){
            var value = $(this).val();
            if ($(this).hasClass('state-schools')){
                var classToShow = value.split('/').slice(-2, -1);
                $('div.schools_in_state_box li').hide();
                var colCount = 0;
                var items = $('div.schools_in_state_box li.' + classToShow).sort(SortByTextContent);
                items.each(function(){
                    $(this).appendTo($('div.schools_in_state_box td ul')[colCount]);
                    colCount = colCount == 0 ? 1 : 0;
                    });
                items.show();
                // $('div.schools_in_state_box li.' + classToShow).show();
            }
            else
			    window.location.href=value;
		});

        var lst_class = this.id.slice(0, -5);
        var selectItems = lst_class.length > 0 ? $("." + lst_class + ' ul li a') : $('>li a', this);
        selectItems = selectItems.sort(SortByinnerHTML);
        // Add default option if needed
        if (lst_class.length > 0) {
            var defaultOption = lst_class.length > 0 ? defaults[lst_class] : '--';
		    var option=$(document.createElement('option'))
			    .appendTo(select).val('#').html(defaultOption);
        }
        var hasItem = false;
        selectItems.each(function(){
            hasItem = true;
			var option=$(document.createElement('option'))
			.appendTo(select)
			.val(this.href)
			.html($(this).html())
		});
        if (lst_class.length > 0)
		    $('.' + lst_class).remove();

		list.remove();

        if(hasItem)
		    select.uniform();
        else{
            $(select).prev().remove();
            select.remove();
        }

	});



	// --------------------
	// --- Tabification ---
	// --------------------



	$(".tabnav a").click(function(event){
		event.preventDefault();
		var tab_id = $(".tabnav a").index(this) + 1;

		$(".tab_active").removeClass("tab_active").addClass("tab_inactive");
		$("#tab_"+tab_id).removeClass("tab_inactive").addClass("tab_active");

		$(".tabnav a.active").removeClass("active");
		$(this).addClass("active");
	});

	//.salaries_box



	// ------------------
	// --- Pagination ---
	// ------------------

	//hide content via - position


	$('.schools_box div table tbody tr').slice(1).quickpaginate({
		perpage: 10,
		showcounter: true,
		pager: $("#school_pager"),
		totalnumber: 3,
		trigger: true,
		prev_text : '<',
      	next_text : '>'
	});


	$('.offline_schools table tbody tr').slice(1).quickpaginate({
		perpage: 20,
		showcounter: true,
		pager: $("#school_pager"),
		totalnumber: 3,
		trigger: true,
		prev_text : '<',
      	next_text : '>'
	});


	$('.salaries_box table tr').slice(1).quickpaginate({
		perpage: 10,
		showcounter: true,
		pager: $("#salary_pager"),
		totalnumber: 3,
		trigger: true,
		prev_text : '<',
      	next_text : '>'
	});

	$('.education_in_state_box table tbody tr').slice(1).quickpaginate({
		perpage: 50,
		showcounter: true,
		pager: $("#education_in_state_pager"),
		totalnumber: 3,
		trigger: true,
		prev_text : '<',
      	next_text : '>'
	});

	$('.city_school_list .school').slice(1).quickpaginate({
		perpage: 10,
		showcounter: true,
		pager: $("#city_school_list_pager"),
		totalnumber: 3,
		trigger: true,
		prev_text : '<',
      	next_text : '>'
	});



	new QuickDegreeFinder().populate('.cd_degree select', '.cd_category select', '.cd_subject select');


})

