/* ***** 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 */
				/* Google Images */
				if(seoposition.ref.indexOf('images.google.') > -1)
				{	console.log('google image');
					this.ssGetGoogleImages ("SEO Google Images","images.google.");
				}
				else if(seoposition.ref.indexOf('google.') > -1)
				{
					console.log('google');
					this.ssGetPageNum ("SEO Google Page",".google.","q=","start=",0,10);
				}
				else if(seoposition.ref.indexOf('images.search.yahoo.') > -1)
				{
					console.log('yahoo');
					this.ssGetYahooImages ("SEO Yahoo Page","images.search.yahoo.");	
				}
				else if(seoposition.ref.indexOf('yahoo.') > -1)
				{
					console.log('yahoo');
					this.ssGetPageNum ("SEO Yahoo Page",".yahoo.","p=","b=",1,10);	
				}
				else if(seoposition.ref.indexOf('images.search.yahoo.') > -1)
				{
					console.log('yahoo image');
					this.ssGetYahooImages ("SEO Yahoo Page","images.search.yahoo.");	
				}
				else if(seoposition.ref.indexOf('bing.com') > -1)
				{
					console.log('bing');
					this.ssGetPageNum ("SEO Bing Page",".bing.","q=","first=",1,10);
				}
				else if(seoposition.ref.indexOf('.aol.') > -1)
				{
					console.log('aol');
					this.ssGetPageNum ("SEO AOL Page",".aol.","query=","page=",1,1);	
				}
				else if(seoposition.ref.indexOf('.ask.') > -1)
				{
					console.log('ask');
					this.ssGetPageNum ("SEO ask Page",".ask.","q=","page=",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.");
			
				
			
		}
}
//http://images.search.yahoo.com/images/view?back=http%3A%2F%2Fimages.search.yahoo.com%2Fsearch%2Fimages%3Fp%3Dadroitcoders%26ei%3DUTF-8%26fr%3Dmoz35%26fr2%3Dtab-web&w=54&h=47&imgurl=adroitcoders.com%2Fimages%2Fani.jpg&rurl=http%3A%2F%2Fadroitcoders.com%2F&size=1k&name=ani+jpg&p=adroitcoders&oid=30138c8442049ee8&fr2=tab-web&no=3&tt=10&sigr=10o2nkkip&sigi=10v8l0ou8&sigb=12ps75p80

//http://images.search.yahoo.com/images/view?back=http%3A%2F%2Fimages.search.yahoo.com%2Fsearch%2Fimages%3Fp%3Dsea%26b%3D41%26ni%3D20%26ei%3DUTF-8%26xargs%3D0%26pstart%3D1%26fr%3Dmoz35%26fr2%3Dtab-web&w=500&h=367&imgurl=farm4.static.flickr.com%2F3110%2F2296385368_6a86b1c466.jpg&rurl=http%3A%2F%2Fwww.flickr.com%2Fphotos%2F99996011%40N00%2F2296385368%2F&size=112k&name=Beach+Nature+Oce...&p=sea&oid=afe05df138405cda&fr2=tab-web&fusr=Tiquetonne20...&no=41&tt=40259034&b=41&ni=20&sigr=11lbhkfen&sigi=11m5m8fuj&sigb=13ckvsjfr

//http://images.search.yahoo.com/images/view?back=http%3A%2F%2Fimages.search.yahoo.com%2Fsearch%2Fimages%3Fp%3Dkale%26b%3D19%26ni%3D18%26ei%3Dutf-8%26y%3DSearch%26xargs%3D0%26pstart%3D1%26fr%3Dmoz35&w=600&h=450&imgurl=www.zentastic.com%2Fblog%2Fwp-content%2Fuploads%2F2009%2F03%2Fkale.jpg&rurl=http%3A%2F%2Fwww.zentastic.com%2Fblog%2F2009%2F03%2F03%2Fcactus-eaters&size=104k&name=kale&p=kale&oid=0f43ee4afcfd7878&fr2=&no=33&tt=422778&b=19&ni=18&sigr=11mlksgj3&sigi=11qnbphsl&sigb=13asugnod
seoposition.ssGetYahooImages = function(source,domain) {
    	if(seoposition.ref.indexOf(domain) > -1) {  
		var imagePerpage = seoposition.ssGetParam("ni",seoposition.ref);
		if(imagePerpage == 0)
		{
			var ssImageNum = 1;
		}
		else
		{
			var ssImageNum = seoposition.ssGetParam("no",seoposition.ref);

			if(ssImageNum >  imagePerpage)
			{
				var clv_modrem = ssImageNum % imagePerpage;
				if(clv_modrem == 0)
					ssImageNum = ssImageNum / imagePerpage; 
				else
					ssImageNum = parseInt(ssImageNum / imagePerpage) + 1; 
			}
			else
			ssImageNum = 1;
		}
		console.log(ssImageNum);
	}
}
// 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);
		if(ssImageNum >  18)
		{
			var clv_modrem = ssImageNum % 18;
			if(clv_modrem == 0)
				ssImageNum = ssImageNum / 18; 
			else
				ssImageNum = parseInt(ssImageNum / 18) + 1; 
		}
		else
		ssImageNum = 1;
		console.log(ssImageNum);
		/*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
// 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}
//this.ssGetPageNum ("SEO Google Page",".google.","q=","start=",0,10);
seoposition.ssGetPageNum = function (source,domain,qParam,pParam,offset,divisor) {
    	var page = 1;
		if(seoposition.ref.indexOf(domain) > -1  && seoposition.ref.indexOf(qParam) > -1 ) {
		page = seoposition.ssCalcPageNum(pParam,offset,divisor);
		
		//if(pageTracker != 'undefined') pageTracker._trackEvent(source, "Page" + page, document.location.pathname, page);
	}console.log(page);
}


// 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
//  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];
		}
	}
	return 0;
}


seoposition.init();