/*                                                                                                                                                      
Copyright (c) 2006, Db Medialab AS. All rights reserved.                                                                                                                                                                                                               
version: 0.1                                                                                                                                         
*/ 

/**
 * The DB Medialab global namespace
 * @constructor
 */
var DBM = window.DBM || {};

/**
 * Returns the namespace specified and creates it if it doesn't exist
 *
 * DBM.namespace("property.package");
 * DBM.namespace("DBM.property.package");
 *
 * Either of the above would create YAHOO.property, then
 * DBM.property.package
 *
 * @param  {String} ns The name of the namespace
 * @return {Object}    A reference to the namespace object
 */
DBM.namespace = function(ns) {

    if (!ns || !ns.length) {
        return null;
    }

    var levels = ns.split(".");
    var nsobj = DBM;

    // DBM is implied, so it is ignored if it is included
    for (var i=(levels[0] == "DBM") ? 1 : 0; i<levels.length; ++i) {
        nsobj[levels[i]] = nsobj[levels[i]] || {};
        nsobj = nsobj[levels[i]];
    }

    return nsobj;
};

DBM.namespace("module");
DBM.namespace("util");
DBM.namespace("util.include");

DBM.util.include.includeFiles = new Array();

DBM.util.include.includeOnce = function (script_filename) {
    if (!DBM.util.include.inArray(script_filename, DBM.util.include.includeFiles)) {
    	DBM.util.include.includeFiles[DBM.util.include.includeFiles.length] = script_filename;
        DBM.util.include.includeDom(script_filename);
    }
}

DBM.util.include.inArray = function (needle, haystack) {
    for (var i = 0; i < haystack.length; i++) {
        if (haystack[i] == needle) {
            return true;
        }
    }
    return false;
}

DBM.util.include.includeDom = function (script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

DBM.util.createDiv = function (target, html) {

	if (target != "") {
		if (document.body.innerHTML){
			   document.getElementById(target).innerHTML = html;
		}
		else if (document.getElementById){
			   var element = document.getElementById(target);
			   var range = document.createRange();
		
			   range.selectNodeContents(element);
			   range.deleteContents();
			   element.appendChild(range.createContextualFragment(html));
		}
	}
}


DBM.util.getObj = function (name) {

	if (document.getElementById)
	{
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all)
	{
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
	else if (document.layers)
	{
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

DBM.util.request = function(id, url, callback, target, loadingTarget, method, formId) {
	this.id = id;
	this.url = url;
	this.callback = callback;
	this.formId = formId;
	this.target = target;
	this.loadingTarget = loadingTarget;
	
	if (method != undefined) {
		this.method = method
	}
	else {
		this.method = "GET";	
	}
	
	this.isStopped = false;
	this.currentRetry = 0;
}


DBM.module.Common = {

	requests: new Array(), 
	//rootUrl: "http://www.dagbladet.no/api/blink/",
	rootUrl: "http://blink.dagbladet.no/gallery/",
	
	defaultLoadingHTML: '<img src="http://gfx.dagbladet.no/comments/loading.gif" />',
	
	retries: 5,
	
	currentRetry: 0,
	
	currentRequestUrl: "",
	
	timeout: 5000,
	
	currentRequest: "",
	
	currentFormId: "",
	
	currentLoadingTarget: "",
	
	currentTarget: "",
	
	currentCallback: "",
	
	currentMethod: "GET",
	
	currentRequestIsStopped: true,
	
	addRequest: function(id, url, callback, target, loadingTarget, method, formId) {

		if (this.requests.length <= 0) {
			this.requests = new Array();	
		}
		
		var request = new DBM.util.request(id.tId, url, callback, target, loadingTarget, method, formId);

		
		//alert("LENGDE" + this.requests.length + " " + id.tId);
		
		//alert(request.url + request);

		this.requests[id.tId] = request;
		
	},
	
	removeRequest: function(id) {
		//this.requests[id] = null;	
		this.requests[id].url = "";
		this.requests[id].formId = "";
		this.requests[id].target = "";
		this.requests[id].loadingTarget = "";
		this.requests[id].callback = "";
		this.requests[id].method = "GET";
		this.requests[id].currentRetry = 0;
		this.requests[id].isStopped = true;
	},
	
	getRequest: function(id) {
		return this.requests[id];
	},
	
	defaultCallback: { 
		success: function(o) {
			
			DBM.module.Common.removeRequest(o.tId);

		
		},  	
		failure: function(o) {

			//alert ("feil for " + o.tId + "\n"  + "på " + DBM.module.Common.requests[o.tId].currentRetry + ". forsøk" + "\n" + DBM.module.Common.requests[o.tId].url);
			
			DBM.module.Common.requests[o.tId].currentRetry
			if (DBM.module.Common.requests[o.tId].currentRetry < DBM.module.Common.retries) {
				DBM.module.Common.requests[o.tId].currentRetry += 1;
				//alert ("Prover igjen for" + DBM.module.Common.requests[o.tId].currentRetry + " gang" + DBM.module.Common.requests[o.tId].method + " " + DBM.module.Common.requests[o.tId].url);
				//var request = YAHOO.util.Connect.asyncRequest(DBM.module.Common.requests[o.tId].method, DBM.module.Common.requests[o.tId].url, DBM.module.Common.requests[o.tId].callback);
				var request = YAHOO.util.Connect.asyncRequest(DBM.module.Common.requests[o.tId].method, DBM.module.Common.requests[o.tId].url, DBM.module.Common.requests[o.tId].callback);

				DBM.module.Common.addRequest(request , 
											 DBM.module.Common.requests[o.tId].url, 
											 DBM.module.Common.requests[o.tId].callback, 
											 DBM.module.Common.requests[o.tId].target, 
											 DBM.module.Common.requests[o.tId].loadingTarget, 
											 DBM.module.Common.requests[o.tId].method);
				DBM.module.Common.requests[request.tId].currentRetry = DBM.module.Common.requests[o.tId].currentRetry;
				DBM.module.Common.removeRequest(o.tId);
				//DBM.module.Common.requests[o.tId].isStopped = false;
			}
			else {
				//alert("fjerner det ");
				DBM.util.createDiv(DBM.module.Common.requests[o.tId].loadingTarget, "");
				DBM.module.Common.removeRequest(o.tId);
			}
		},  	
		timeout: this.timeout 	 
	}
	
}



// Show the complaint form
YAHOO.widget.Effect = function(el) {
	this.oEl = YAHOO.util.Dom.get(el);
	this.height = parseInt(YAHOO.util.Dom.getStyle(this.oEl,'height'));
	this.width = parseInt(YAHOO.util.Dom.getStyle(this.oEl,'width'));
};

YAHOO.widget.Effect.prototype.BlindUp = function(iTimer, onComplete) {
	var timer = iTimer || 0.3;
	this.oEl.style.overflow = 'hidden';
	var blind = new YAHOO.util.Anim(this.oEl, { height: { to:0} }, timer, YAHOO.util.Easing.easeOut);
	if ( onComplete ) {
		blind.onComplete.subscribe(onComplete);
	}
	blind.animate();
};

YAHOO.widget.Effect.prototype.BlindDown = function(toHeight, iTimer, onComplete) {
	this.oEl.style.visibility = 'hidden';
	this.oEl.style.overflow = 'hidden';
	this.oEl.style.height = '0';
	//var height = parseInt(YAHOO.util.Dom.getStyle(this.oEl,'height'));
	this.oEl.style.height = '';
	this.oEl.style.visibility = 'visible';
	var timer = iTimer || 0.3;
	var blind = new YAHOO.util.Anim(this.oEl, { height: { to:toHeight, from:0} }, timer, YAHOO.util.Easing.easeOut);

	var that = this;
	blind.onComplete.subscribe(
		function() {
			that.oEl.style.overflow = '';
			//alert(that.oEl.style.overflow);
		}
	);
	/*if ( onComplete ) {
		blind.onComplete.subscribe(onComplete);
	}*/
	blind.animate();
};

YAHOO.widget.Effect.prototype.yoyo = function(text, iTimer, onComplete) {
	//this.oEl.style.overflow = 'hidden';
	var height = parseInt(YAHOO.util.Dom.getStyle(this.oEl,'height'));
	var timer = iTimer || 0.3;
	var yoyo = new YAHOO.util.Anim(this.oEl, { height: { to:0} }, timer, YAHOO.util.Easing.easeOut);
	var that = this;
	yoyo.onComplete.subscribe(
		function() {
			that.oEl.style.visibility = 'hidden';
			//that.oEl.style.overflow = 'hidden';
			that.oEl.style.height = '';
			that.oEl.innerHTML = text;
			var height = parseInt(YAHOO.util.Dom.getStyle(that.oEl,'height'));
			that.oEl.style.height = '0';
			that.oEl.style.visibility = 'visible';
			var blind = new YAHOO.util.Anim(that.oEl, { height: { to:height, from:0} }, timer, YAHOO.util.Easing.easeOut);
			if ( onComplete ) {
				blind.onComplete.subscribe(onComplete);
			}
			blind.animate();
		}
	);
	yoyo.animate();
};
