/* vk-init */

var jQueryExtend = function() {
    /*
    $.extend({
        getFachFunct: function() {
		var fach = null, pathname;
	    	pathname = window.location.pathname;
		if (pathname.match(/\/alle\//)) {
		    fach = "alle";
		}
		else if (pathname.match(/\/medien\//)) {
		    fach = "medien";
		}
		else if (pathname.match(/\/theater\//)) {
		    fach = "theater";
		}
		else if (pathname.match(/\/film\//)) {
		    fach = "film";
		}
		else {
		    fach = "alle";
		}
		return(function(){return(fach)});
            },

        getVKTypeFunct: function(){
		var type = null, pathname = window.location.pathname;		
		if (pathname.match(/\/bibliothekskataloge\//)) {
		    type = "bib";
		}
		else if (pathname.match(/\/aufsatzdatenbank\//)) {
		    type = "auf";
		}
		else if (pathname.match(/\/nationallizenzen\//)) {
		    type = "nat";
		}
		else if (pathname.match(/\/bibliographien\//)) {
		    type = "bibl";
		}
		else if (pathname.match(/\/metasuche\//)) {
		    type = "all";
		}
	        *
		else if (pathname.match(/\/extern\//)) {
		    type = "all";
		}
		*
		else if (pathname.match(/\/suche\//)) {
		    type = "all";
		}
		return(function(){return(type)});
            },

        isCombiSearchFunct: function() {
	        var isCombiSearch = false,
		pathname = window.location.pathname;		
		if (pathname.match(/\/suche\//)) {
		    isCombiSearch = true;
		}
		return(function() {return(isCombiSearch)});
            }
        });
    */
    var getFachFunct = function() {
	var fach = null, pathname;
	pathname = window.location.pathname;
	if (pathname.match(/\/alle\//)) {
	    fach = "alle";
	}
		else if (pathname.match(/\/medien\//)) {
		    fach = "medien";
		}
	else if (pathname.match(/\/theater\//)) {
	    fach = "theater";
	}
	else if (pathname.match(/\/film\//)) {
	    fach = "film";
	}
	else {
	    fach = "alle";
	}
	return(function() {
	    return(fach)
	});
    },
    getVKTypeFunct = function(){
	var type = null, pathname = window.location.pathname;		
	if (pathname.match(/\/bibliothekskataloge\//)) {
	    type = "bib";
	}
	else if (pathname.match(/\/aufsatzdatenbank\//)) {
	    type = "auf";
	}
	else if (pathname.match(/\/nationallizenzen\//)) {
	    type = "nat";
	}
	else if (pathname.match(/\/bibliographien\//)) {
	    type = "bibl";
	}
	else if (pathname.match(/\/metasuche\//)) {
	    type = "all";
	}
	else if (pathname.match(/\/suche\//)) {
	    type = "all";
	}
	return(function() {
	    return(type)
	});
    },
    isCombiSearchFunct = function() {
	var isCombiSearch = false,
	pathname = window.location.pathname;		
	if (pathname.match(/\/suche\//)) {
	    isCombiSearch = true;
	}
	return(function() {
	    return(isCombiSearch)
	});
    };

    $.extend({
	getUrlVars: function(){
		var i, vars, hash, hashes, location;
		vars = {};
                location = 
                    window.location.href.replace(new RegExp(/#[^#]*$/), "");
                hashes = location.slice(location.indexOf('?') + 1).split('&');
		for(i = 0; i < hashes.length; i++)
		{
		    hash = hashes[i].split('=');
		    if (vars[hash[0]]) {
			vars[hash[0]].push(hash[1]);
		    }
		    else {
			vars[hash[0]] = [hash[1]];
		    }
		}
		return vars;
	    },
	getUrlVar: function(name){
		return $.getUrlVars()[name];
	    },
        getUrlVarFirstVal: function(name){
                var vals = $.getUrlVar(name);
                if (vals) {
                    return vals[0];
                }
                else {
                    return(null);
                }
            },

	/*
        getFach: $.getFachFunct(),
        getVKType: $.getVKTypeFunct(),
        isCombiSearch: $.isCombiSearchFunct()
	*/
        getFach: getFachFunct(),
        getVKType: getVKTypeFunct(),
        isCombiSearch: isCombiSearchFunct()
	});

};


var browserSupported = function() {
    var isMSI = false, supported = true;
    $.each($.browser, function(i, val) {
	    // alert(i + ' : ' + val);
	    if (i == 'msi') {
		isMSI = true;
	    }
	    else if (i == 'version') {
		if (val == '6.0') {
		    supported = false;
		}
		else if (val == '7.0') {
		    supported = false;
		}
	    }
	});
    if (!supported) {
	alert('Internet Explorer Versionen 6.0 und 7.0 werden noch nicht ' +
	      ' unterstützt');
    }
    return(supported);
    // return(true);
};


var onDocumentReady = function() {
    var catId,
    catIds,
    container,
    i, keys, vals, ops, empty,
    // searcher, cat, catVw, 
    lang,
    mode,
    //combiMode,
    //combiMode = "indexSearch",
    combiMode = "externSearch",
    vkBasket,
    vkIndexCats,
    vkCats,
    vkLang,
    vkMat,
    vkQuery,
    vkQueryHistory,
    vkSearcher,
    vkVw,
    mat,
    mats,
    query,
    queryStr,
    fqss,
    fqs,
    sid,
    allVars,
    name, val,
    styles,
    indexMode;

    jQuery.ajaxSettings.traditional = true; // fq instead of fq[]

    jQueryExtend();
    allVars = $.getUrlVars();

    //vkConfCatsTreeInit();

    // if (browserSupported()) {
    if (true) {

    // sid = VKSid();

    styles = allVars.style || [];
    vkStyles = VKStyles({styles: styles});
    
    lang = allVars.lang ? allVars.lang[0] : "";

    mode = allVars.m ? allVars.m[0] : "";
     // mode = "simpleSearch";
     // mode = "advancedSearch";
     // mode = "basket";
     // mode = "help";
    if (mode === 'einfache-suche') {
	mode = 'simpleSearch';
    }
    else if (mode === 'erweiterte-suche') {
	mode = 'advancedSearch';
    }
    else if (mode === 'merkliste') {
	mode = 'basket';
    }

    if ($.isCombiSearch()) {
	indexMode = (allVars.hasOwnProperty('index')) ? 
	    parseInt(allVars.index[0], 10) : 1;
	if (indexMode === 0) {
	    combiMode = "externSearch";
	}
	else if (indexMode === 1) {
	    combiMode = "indexSearch";
	}
    }

     vkMat = VKMat({});

     vkSearcher = VKSearcher({url: 'searcher/search'});

     vkIndexCats = VKIndexCats({
	 cats: vkConfCats,
	 tree: vkConfCatsIndexTreeInit(),
	 searcher: vkSearcher});

     vkCats = VKCats({
	 cats: vkConfCats,
	 tree: vkConfCatsTreeInit(),
	 searcher: vkSearcher});

     vkBasket = VKBasket({cats: vkCats});

     vkQuery = null;
     vkQueryHistory = VKQueryHistory({});

     if (mode == "") {
	 queryStr = decodeURIComponent(allVars.q ? allVars.q[0] : "");
	 catIds = allVars.c;
	 // alert(cats);
	 if (catIds) {
	     if (combiMode === "externSearch") {
		 vkCats.selectByIds(catIds);
		 cats = vkCats.selectedGet();
	     }
	     else if (combiMode === "indexSearch") {
		 vkIndexCats.selectByIds(catIds);
		 cats = vkIndexCats.selectedGet();
	     }
	     else {
		 vkCats.selectByIds(catIds);
		 cats = vkCats.selectedGet();
	     }
	 }
	 fqss = decodeURIComponent(allVars.fqs ? allVars.fqs[0] : "");
	 fqs = (fqss !== "") ? jQuery.parseJSON(fqss) : null; 
	 if (queryStr != "") {
	     query = VKQuery({keys: ["all"],
			      vals: [queryStr],
			      ops: [],
			      mats: [],
			      cats: cats,
			      fqs: fqs});
	     vkQuery = query;
	     mode = "simpleSearch";
	 }
	 else {
	     empty = true;
	     keys = [];
	     vals = [];
	     ops = [];
	     for (i = 0; i < 3; i++) {
		 keys[i] = $.getUrlVarFirstVal('k'+i) || "";
		 vals[i] = 
		     decodeURIComponent($.getUrlVarFirstVal('v'+i) || "");
		 if (vals[i] != "") {
		     empty = false;
		 }
		 ops[i] = decodeURIComponent($.getUrlVarFirstVal('o'+i)) || "";
	     }
	     if (!empty) {
		 mat = allVars.mat ? allVars.mat[0] : "";
		 mats = vkMat.matsFromUrlParam(mat);
		 vkMat.matsSet(mats);
		 query = VKQuery({keys: keys,
				  vals: vals,
				  ops: ops,
				  mats: mats,
				  cats: cats,
				  fqs: fqs});
		 vkQuery = query;
		 mode = "advancedSearch";
	     }
	 }
     }

     /**/
     vkLang = VKLang({lang: lang, langs: VKLangs});
     mt = vkLang.text;
     /**/

     if (!((mode == "simpleSearch") ||
	   (mode == "advancedSearch") ||
	   (mode == "basket")/* ||
  	   (mode == "help")*/)) {
	 mode = "simpleSearch";
     }

     container = $("div.vkPage");
     vkVw = VKViewPage({
	 basket: vkBasket,
	 indexCats: vkIndexCats,
	 cats: vkCats,
	 container: container,
	 lang: vkLang,
	 mat: vkMat,
	 mode: mode,
	 combiMode: combiMode,
         query: vkQuery,
         queryHistory: vkQueryHistory,
	 searcher: vkSearcher});

    if (lang) {
	vkLang.set(lang);
    }

    // $("div.coins.seed").remove();

    window.vkCoins = VKCoins();

    } // browserSupported
};


$(document).ready(onDocumentReady);
