
function $(el){
	if (typeof el == 'string'){
		el = document.getElementById(el);
	}
	return el;
};

Function.prototype.delegate = function(target, args, passorginal) {
	var _this = this;
	var retFunction = function() {
		var passArgs = new Array();
		if(passorginal === true) {
			if(arguments !== null && arguments != undefined) {
				for(var i=0;i<arguments.length;i++) {
					passArgs.push(arguments[i]);		
				}
			}
		}
		if(args !== null && args != undefined) {
			passArgs = passArgs.concat(args);
		}		
		_this.apply(target, passArgs);
	}
	return retFunction;
};

var dsgn_get_xhr_request = function (url, statechangehandler) {

	var req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = statechangehandler;
		req.open("POST", url, true);
	}
	
	return req;	
};

var dsgn_load_method = function() {

	dsgn_mobile_orientationChange();
	
	var mainImage = document.getElementById('mainimage');
	if(mainImage !== null && mainImage !== undefined) {
		mainImage.setAttribute('width', '100%');
		mainImage.removeAttribute('height');
	}
};

var dsgn_mobile_orientationChange = function() {

	var orient = 0;
	if(window.orientation !== undefined && window.orientation !== null) {
		orient = (Math.abs(window.orientation) === 90 || Math.abs(window.orientation) === -90) ? 'landscape' : 'portrait'; 		
	} else {
		orient = 'landscape';
	}
	 var cl = document.body.className; 
	 cl = cl.replace(/portrait|landscape/, orient);
	 document.body.className = cl;
};

var current_favorite_xhr = null;

var dsgn_add_to_favorites = function(target, itemid, hide_id, show_id) {
	
	var req = dsgn_get_xhr_request('/ajax/JsonCall.php', dsgn_add_favorites_statechange_handler.delegate(window, [target, itemid, hide_id, show_id], false));
	if(req !== false) {
		req.setRequestHeader('X-Request', 'JSON');
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
		
		current_favorite_xhr = req;
		
		req.send('json {"action":"DSGNAddFavorite","fid":'+itemid+'}');
	}
};

var dsgn_remove_from_favorites = function(target, itemid, hide_id, show_id) {

	var req = dsgn_get_xhr_request('/ajax/JsonCall.php', dsgn_remove_favorites_statechange_handler.delegate(window, [target, itemid, hide_id, show_id], false));
	if(req !== false) {
		req.setRequestHeader('X-Request', 'JSON');
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
		
		current_favorite_xhr = req;
		
		req.send('json {"action":"DSGNRemoveFavorite","fid":'+itemid+'}');
	}
};

var dsgn_add_favorites_statechange_handler = function(target, itemid, hide_id, show_id) {
	var req = current_favorite_xhr;
	if(req === null || req === undefined) {
		return;
	}
	if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			document.getElementById(hide_id).style.display = 'none';
			document.getElementById(show_id).style.display = 'block';
        } else {
            alert("There was a problem adding this article to your favorites");
        }
    }
};

var dsgn_remove_favorites_statechange_handler = function(target, itemid, hide_id, show_id) {
	var req = current_favorite_xhr;
	if(req === null || req === undefined) {
		return;
	}
	if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			document.getElementById(hide_id).style.display = 'none';
			document.getElementById(show_id).style.display = 'block';
        } else {
            alert("There was a problem removing this article to your favorites");
        }
    }
};
