/** Fix Issues in IE */
function fixesForIE(){
	/** Add filter method to the Array implementation for IE */
	if (!Array.prototype.filter){
	  Array.prototype.filter = function(fun)  {
	    var len = this.length;
	    if (typeof(fun) != "function"){
	      throw new TypeError();}
	    var res = [];
	    var thisp = arguments[1];
	    for(var i = 0; i < len; i++){
	      if (i in this){
	        var val = this[i];
	        if (fun.call(thisp, val, i, this)){
	          res.push(val);}
	      }
		 }
		
	    return res;
	  };
	}
	/* Add Array.indexOf to IE */
	if(!Array.indexOf){
		Array.prototype.indexOf = function(obj){
			for(var i=0,l=this.length;i<l; i++){
				if(this[i]==obj){return i;}
			}
			return -1;
		}
	}

}

/** Fix Issues in Mozilla Based Browsers */
function fixesForGecko(){
	
}


/** Browser Detection Object */
var Browser = {
    IE:     !!(window.attachEvent && !window.opera),
    Opera:  !!window.opera,
    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
    Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
};


/** Cross Browser Event Handling */
function addEvent(node, evnt, handler){ 
 if (node.addEventListener){ 
   node.addEventListener(evnt, handler, false); 
   return true; 
 } else if (node.attachEvent){ 
   var r = node.attachEvent("on"+evnt, handler); 
   return r; 
 } else { 
   return false; 
 } 
}


addEvent(window,"load",function(){
	fixesForIE();
	fixesForGecko();
});

/** Short For document.getElementById,creareElement,createTextNode */
if(typeof(jQuery) == 'undefined'){
		window.$ = function(node){
		 if(typeof(node) == 'string'){
		 	var elm = document.getElementById(node);
		 	if(!elm){
		 		elm = document.getElementsByName(node);
		 		if(elm.length == 0) elm = null;
		 		else elm = elm[0];
		 	}
		 	return elm;
		 }
		 else if(typeof(node) == 'object')
			return node;
		 else return null;
		}
}
/** Create Element and Create TextNode */
function ce(el){return document.createElement(el);}
function tn(text){return document.createTextNode(text);}


/** Node CSS-Class Manipulation Functions */
function hasClass(ele,cls){return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));}
function addClass(ele,cls){if(!this.hasClass(ele,cls)){ ele.className += " "+cls;}}
function removeClass(ele,cls){
	if(hasClass(ele,cls)){
	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
	ele.className=ele.className.replace(reg,' ');}
}

/** Function to Toggle visibility of a DOM Node*/
function toggleNode(nodeID){
	var node = $(nodeID);
	if(node.style.display == "" || node.style.display == null) node.style.display = "none";
	else node.style.display = "";
}
function showNode(nodeID){
	if($(nodeID))
		$(nodeID).style.display = "";
}
function hideNode(nodeID){
	if($(nodeID))
		$(nodeID).style.display = "none";
}

/** Node and Childrem Removal Utils */
function removeNode(nodeId){
	var node = $(nodeId);
	if(node && node.parentNode)
		node.parentNode.removeChild(node);
}
function removeChildren(nodeId){
	var node = $(nodeId);
	while(node.hasChildNodes())
		node.removeChild(node.lastChild);
}

/** Cookie Handling Functions */
function readCookie(n){
	var nEQ = n + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++){
		var c = ca[i];
		while (c.charAt(0)==' ') 
			c=c.substring(1,c.length);
		if (c.indexOf(nEQ) == 0)
			return unescape(c.substring(nEQ.length,c.length));
	}
	return null;
}
function saveCookie(Name,Value){
	var expiryDate = new Date();
	var offset=1/1;
	expiryDate.setYear(expiryDate.getFullYear()+offset);
	document.cookie = escape(Name) + "=" + escape(Value) + "; expires=" + expiryDate.toGMTString() + "; path=/;";
}

/** Open external url through dir.guruji.com **/

function mw(u,params){
	var url=new Array();
	url.push("http://dir.guruji.com/redirectm.php?target=");
	url.push(escape(unescape(u).replace(/&amp;/g,'&')));
	if(params){
		for(var param in params){
			url.push("&");
			url.push(param);
			url.push("=");
			url.push(params[param]);
		}
	}	
	var windowleft=parseInt((screen.width/2)-250);
	var windowtop=parseInt((screen.height/2)-200);
	try{
		var muW = window.open(url.join(''),'musicwindow','scrollbars=yes,width=500,height=400,left='+ windowleft+
						',top='+windowtop+',screenX='+windowleft+',screenY='+windowtop+'');
		window.focus();
		muW.focus();
	}catch(e){
		//alert(e);
	}
}


function dw(u,params){
	var url=new Array();
	url.push("http://dir.guruji.com/redirectmp3.php?target=");
	url.push(escape(unescape(u).replace(/&amp;/g,'&')));
	if(params){
		for(var param in params){
			url.push("&");
			url.push(param);
			url.push("=");
			url.push(params[param]);
		}
	}	
	var windowleft=parseInt((screen.width/2)-250);
	var windowtop=parseInt((screen.height/2)-200);
	try{
		var muW = window.open(url.join(''),'downloadwindow','scrollbars=yes,width=500,height=400,left='+ windowleft+
						',top='+windowtop+',screenX='+windowleft+',screenY='+windowtop+'');
		window.focus();
		muW.focus();
	}catch(e){
		//alert(e);
	}
}


/*
 function mw(u,params){
	var url=new Array();
	url.push("http://dir.guruji.com/redirect.php?target=");
	url.push(escape(unescape(u).replace(/&amp;/g,'&')));
	if(params){
		for(var param in params){
			url.push("&");
			url.push(param);
			url.push("=");
			url.push(params[param]);
		}
	}	
	var windowleft=parseInt((screen.width/2)-250);
	var windowtop=parseInt((screen.height/2)-200);
	dest = 	url.join('');

try{
var my_window = window.open("",'musicwindow','scrollbars=yes,width=500,height=400,left='+ windowleft+',top='+windowtop+',screenX='+windowleft+',screenY='+windowtop+'');
my_window.document.write('<font size=2 face=arial>Redirecting to: <BR><div style="color:gray>"<B>'+String(unescape(u).replace(/&amp;/g,'&')).substr(0,50)+'...</B><BR><BR><span id="sec_w">in <span id="seconds">5</span> seconds</span></div><BR><HR><BR>Don\'t want to wait?<BR><a href="'+dest+'">Click here to Play</a></font>');  
	}catch(e){
		//alert(e);
	}
	c = 6;
	s = setInterval(function(){
	--c;
	if(c == 0){
		try{
			my_window.document.getElementById('seconds').innerHTML = "";
			my_window.document.getElementById('sec_w').innerHTML = "<B>Opening...</B><BR>Please wait...";}catch(e){clearInterval(s);}
			clearInterval(s);

		}
	else{
		if(c == 2){
			my_window.location.href = dest;
		}
		try{
			my_window.document.getElementById('seconds').innerHTML = c;}catch(e){clearInterval(s);}
		}
	},1000);
	
}

*/
/** Functions for window dimensions and Node Co-ordinates **/
function findPos(obj) {// Help : quirksmode.org
	var curleft = curtop = 0;
	if (obj.offsetParent){
		do{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}while(obj=obj.offsetParent);
	}
	return [curleft,curtop];
}

function vScrollTop(){// Help : quirksmode.org
	if (document.documentElement && document.documentElement.scrollTop)
		theTop = document.documentElement.scrollTop;
	else if (document.body)
		theTop = document.body.scrollTop;
	else
		theTop = $('body')[0].scrolltop;
	return theTop;
}

function vInnerHeight(){
	var x=0;
	if(self.innerHeight)
		x = self.innerHeight;
	else if(document.documentElement && document.documentElement.clientHeight)
		x = document.documentElement.clientHeight;
	else if (document.body)
		x = document.body.clientHeight;
	return x;
}

function vInnerWidth(){
	var x=0;
	if(self.innerWidth)
		x = self.innerWidth;
	else if(document.documentElement && document.documentElement.clientWidth)
		x = document.documentElement.clientWidth;
	else if (document.body)
		x = document.body.clientWidth;
	return x;
}

/** Text Manipulation Function **/
function capitalize(sentence){
	if(!sentence || sentence =="") return sentence;
	var capSentence = sentence;
	var words = sentence.split(/[ ,.]/);
	for(word in words){	
		var capWord = words[word].substring(0,1).toUpperCase()+words[word].substring(1).toLowerCase();
		capSentence = capSentence.replace(words[word],capWord);
	}
	return capSentence;
}

/** Function for Reflection */
function reflectIt(Obj){
	var reflection = "",member ;
	for(member in Obj){
		reflection += member+"="+Obj[member]+" \n";
	}
	return reflection;
}

/** Error Logging on Server Side **/
function handleError(msg,url,ln) {
    var loggerURL = "http://dir.guruji.com/images/fe_log?" 
    		+ "Url=" + escape(location.pathname)
    		+ "&queryString=" + escape(location.search) 
    		+ "&HTTPRef=" + escape(document.referrer)
    		+ (msg?"&errMsg="+escape(msg):"") 
    		+ (ln?"&errLine="+ln:"") 
    		+ ((Error && (new Error).stack)?"&errStack="+(new Error).stack:"");
		
    (new Image()).src = loggerURL;
}
//addEvent(window,"error",handleError);

function GetInnerSize(){
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return [x,y];
}

window.onerror = handleError;

function moreImageDetails(q,thmb,siz,idm,imgloc,siteloc,s){
	var next="/image?imgurl="+encode(imgloc)+"&imgwpurl="+encode(siteloc)+"&thmburl="+encode(thmb)+"&imgsize="+siz+"&dimension="+idm+"&q="+q+"&ss=1&s="+s;
	//console.log("Next Location: %s",next);
	window.location.href = next;

	return false;
}
