/* ***** 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 and Added More SEs
 * ***** END LICENSE BLOCK ***** */

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

				/* Google */
				this.ssGetPageNum ("Google",".google.","q=","start=",0,10);
				
				/* Yahoo */
				this.ssGetPageNum ("Yahoo",".yahoo.","p=","b=",1,10);
				
				/* MSN */
				this.ssGetPageNum ("MSN",".msn.","q=","first=",1,10);
				
				/* Live */
				this.ssGetPageNum ("Live",".live.","q=","first=",1,10);
				
				/* AOL */
				this.ssGetPageNum ("AOL",".aol.","query=","page=",1,1);		

				/* Google TLD */
				this.ssGetTld ("Google TLD",".google.");

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


// 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 "."

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




// 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 ) {
		if(seoposition.ref.indexOf(pParam) > -1) {
			if(seoposition.ref.split("?").length) {					
				var arParams = seoposition.ref.split("?")[1];							
				arParams = arParams.split("&");
				for(var i=0;i<arParams.length;i++) {
					if(arParams[i].indexOf(pParam) > -1 ) {
							seoposition.ssLogPageNum(source,1+(arParams[i].split("=")[1]-offset)/divisor);
					}
				}
			} 
		} else {
				seoposition.ssLogPageNum(source,1);
		}
	}
}



seoposition.ssLogPageNum =  function (source,page) {
	if(pageTracker != 'undefined') pageTracker._trackEvent(source, "Page" + page, document.location.pathname, page);
}

seoposition.init();