﻿var torget_segment;
var torget_url;
//jQuery.noConflict();
jQuery(document).ready(function(){

    jQuery("#foretagstorget").append('<div id="ajax_loading"></div>');
	
    // Initialize history plugin.
    // The callback is called at once by present location.hash.
    if(!jQuery.history) jQuery = $;
    jQuery.history.init(pageload);
    
});

// PageLoad function
// This function is called when:
// 1. after calling jQuery.historyInit();
// 2. after calling jQuery.historyLoad();
// 3. after pushing "Go Back" button of a browser
function pageload(hash) {
    jQuery("#foretagstorget_info_content").remove();
    jQuery("#foretagstorget_list_content").remove();
    if(hash) {
        if (hash.indexOf("search=") > -1 && hash.indexOf("term=") > -1) {
            var search = hash.substring(hash.indexOf("search=")+7, hash.indexOf("&term="));
            var term = hash.substring(hash.indexOf("term=")+5, hash.length);
            search_foretag(search, term);
        } else {
            get_foretag_detail(hash);
        }
    } else {
        // start page
        get_foretag_list();
    }
}

function callForetagList(url) {
    jQuery("#ajax_loading").show();
    jQuery.ajax({
            type: "GET",
            cache: false,
            url: url,
            scriptCharset: "utf-8",
            dataType: "jsonp",
            success: function(data, textStatus){
                jQuery("#ajax_loading").hide();
                jQuery("#foretagstorget").append(data.html);	
                attach_to_list();
            }
    });

}

function get_foretag_list() {
    callForetagList(torget_url + "content/all-nodes?callback=?&torget_segment=" + torget_segment);
}

function search_foretag(query, term) {
    callForetagList(torget_url + "content/all-nodes?callback=?&torget_segment=page_1&search=" + encodeURIComponent(query) + "&term=" + term);
}

function attach_to_list() {
    jQuery("#foretagstorget").find("a:not(.foretag_popup)").each(function () {
        var alias = this.pathname.substr(this.pathname.lastIndexOf("/")+1);
        this.href = "#" + alias;
    });
    
    jQuery("#foretagstorget").find("a:not(.foretag_popup)").click(function(){
        var hash = this.href;
        hash = hash.replace(/^.*#/, '');
        jQuery.history.load(hash);
        return false;
    });
    
	jQuery("#btn_foretag_search").click(function () { 
		var search = jQuery("#inp_foretag_search").val();
		var term = jQuery("#termselect").val();
		jQuery("#foretagstorget_list_content").remove();
        jQuery.history.load("search="+search+"&term="+term);
		return false;
	});
    
    //make enter keypress trigger search
    jQuery("#foretagstorget input, #foretagstorget select").live('keypress', function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            jQuery('#btn_foretag_search').click();
            return false;
        } else {
            return true;
        }
    });
    jQuery("#inp_foretag_search").focus();
}

function get_foretag_detail(alias) {
    jQuery("#ajax_loading").show();
    jQuery.ajax({
            type: "GET",
            cache: false,
            url: torget_url + "content/foretagssida-jsonp?alias=foretag/" + alias + "&callback=?",
            scriptCharset: "utf-8",
            dataType: "jsonp",
            success: function(data, textStatus){
                jQuery("#ajax_loading").hide();
                jQuery("#foretagstorget").append(data.html);
                window.scrollTo(0,0);
                //window.location.hash = alias;
                jQuery("#foretagstorget").find("a.back_link").click(function () { 
                    history.go(-1);
                    return false;
                });
            }
    });
} 