var shareMedia = {
	iwiw: "http://iwiw.hu/pages/share/share.jsp?u=",
	facebook: "http://www.facebook.com/share.php?u=",
	myspace: "http://www.myspace.com/index.cfm?fuseaction=postto&u=",
	twitter: "http://twitter.com/home?status="
}

function openSharePopup(url,prp){
	var windowName = null;
	try{
		if ((typeof navigator[windowName] != "undefined") && (navigator[windowName] != null)){
			if (navigator[windowName].closed) navigator[windowName] = window.open(url,"",prp);
			else navigator[windowName].location.href = url;
		}else{
			navigator[windowName] = window.open(url,"",prp)
		}
	}catch (err){
		if ((typeof navigator[windowName] == "undefined") || (navigator[windowName]==null)){
			navigator[windowName] = window.open(url,"",prp);
		}else{
			navigator[windowName].location.href = url;
		}
	}
	navigator[windowName].focus();
	return navigator[windowName]
}

function shareCoding(str){
	var ret = '';
	str = unescape( encodeURIComponent(str)); 
	for(var i = 0; i < str.length; i++){
		var chr = str.charCodeAt(i).toString("16");
		ret += chr.length < 2 ? "0"+chr : chr
	}
	return ret;
}

var shareWindow
function share(share_, media_, url_, title_, desc_, img_){
	if(!share_)share_ = "share.php"
	if(!media_)media_ = "iwiw"
	if(!url_)url_ = window.location.href.toString()
	
	shareWindow = openSharePopup("about:blank")
	
	var url = "";
	switch(media_){
		case "myspace" :
			url += encodeURIComponent(url_)
			if(title_)url += "&t="+encodeURIComponent(title_)
			if(desc_)url += "&c="+encodeURIComponent((img_ ? "<img src='"+img_+"' style='float:left'/>" : "")+"<p>"+desc_+"</p>")
			shareWindow.location.href = shareMedia[media_]+url;
		break;
		default:
			url += share_
			url += "?u="+shareCoding(url_);
			if(title_)url += "&t="+shareCoding(title_);
			if(desc_)url += "&d="+shareCoding(desc_);
			if(img_)url += "&i="+shareCoding(img_);
			jQuery.getJSON(share_+'?q='+encodeURIComponent(url), {}, function($data){
				if($data.statusCode=='OK') {
					var bitlyurl = '';
					for(var i in $data.results) {
						bitlyurl = $data.results[i].shortUrl;
					}
					shareWindow.location.href = shareMedia[media_]+escape(bitlyurl);
				} else {
					alert('Error by share!');
				}
			});
		break;
	}
}