// ==UserScript==
// @name          MSN Search for PDC Developers
// @namespace     http://search.msn.com
// @description	PDC MSN Search Hack
// @include       http://search.msn.com/results.aspx*
// @version	0.9.9
// This code is governed by the MICROSOFT SHARED SOURCE CLI, C#, AND JSCRIPT LICENSE
// http://msdn.microsoft.com/msdn-files/027/001/901/ShSourceCLIbetaLicense.htm
// ==/UserScript==


(function() {


// Tracking vars for search params
var siteSearch = false;
var freshness = false;
var popularity = false;
var precision = false;
var blogOnly = false;
var msdnOnly = false;
var queryText = '';

function parseParams() {
	var qString = false;
	var query = false;
	var qString = document.getElementById("q").value; //window.location.search.split("&");
	var specialClause;
	/*
	for(var i=0;i<sString.length;i++) {
		if(sString[i].indexOf("q=") > -1) {
			qString = sString[i].substr(3,sString[i].length-2);
			
		}
	}
	qString = unescape(qString);
	*/
	while(qString.indexOf("%20") > -1) {
		qString = qString.replace("%20","+");
	}
	while(qString.indexOf("+") > -1) {
		qString = qString.replace("+"," ");
	}


	query = qString.split(" ");
	
	for(var j=0;j<query.length;j++) {
		//alert(query[j] + 'frsh:' + query[j].indexOf("{frsh") );
		//alert(j +  ': ' + query[j] + ' of ' + query.length);
		specialClause = false;
		// Parse ranking operators
		if(query[j].indexOf("{") > -1 ) {
			if(query[j].indexOf("{frsh") > -1) {
				freshness = parseInt(query[j].split("{")[1].split("=")[1]);				
				specialClause = false;
				//alert(freshness);
			
			}
			if(query[j].indexOf("{popl") > -1) {
				popularity = parseInt(query[j].split("{")[1].split("=")[1]);		
				specialClause = false;		
			}
			if(query[j].indexOf("{mtch") > -1) {
				precision = parseInt(query[j].split("{")[1].split("=")[1]);				
				specialClause = false;
			}

		}
		// Parse site operators
		if(query[j].indexOf("site:") > -1) {
			if(query[j].indexOf("site:msdn.")) {
				msdnOnly = true;
				specialClause = false;
			}
		}
		
		// Parse hasfeed:
		if(query[j] == "hasfeed:") {
			blogOnly = true;
			specialClause = false;
		}
		
		if(query[j] == "(site:msdn.com | site:www.msdn.com | msdn.microsoft.com)") {
					msdnOnly = true;
					specialClause = false;
		}
		
		if(!specialClause) {
			if(query[j].indexOf("{") > -1) {
				queryText = queryText + query[j].split("{")[0] + ' ';
			} else {
				queryText = queryText + query[j] + ' ';
			}
		}
	}
	//alert(popularity + ' ' +  precision + ' ' + freshness + ' with queyr of ' + queryText);

}

function doMode(mode, direction) {
	//alert(mode + ' with ' + queryText + ' and msdn ' + msdnOnly + ' vs blog ' + blogOnly);
	queryAugment = '';
	if(freshness && mode != 'frsh') queryAugment += ' {frsh='+freshness+'}';
	if(popularity && mode != 'popl') queryAugment += ' {popl='+popularity+'}';
	if(precision && mode != 'mtch') queryAugment += ' {mtch='+precision+'}';
	
	if(mode == "msdn" && !msdnOnly) {
		document.location = 'http://search.msn.com/results.aspx?q='+ escape(queryText + queryAugment) + ' (site:msdn.com | site:www.msdn.com | msdn.microsoft.com)';
	} 
	if(mode == "msdn" && msdnOnly) {
		queryText = queryText.replace("(site:msdn.com | site:www.msdn.com | msdn.microsoft.com)","");
		//alert('reseting msdn '+ escape(queryText + queryAugment) );
		document.location = 'http://search.msn.com/results.aspx?q='+ escape(queryText + queryAugment);
	} 
	
	if(mode == "blog" && !blogOnly) {

		document.location = 'http://search.msn.com/results.aspx?q='+ escape(queryText + queryAugment) +' hasfeed:';
	}

	if(mode == "blog" && blogOnly) {
		queryText = queryText.replace("hasfeed:","");
		document.location = 'http://search.msn.com/results.aspx?q='+ escape(queryText + queryAugment);
	}

	// FRSH
	if(mode == "frsh" && direction == -1) {
		document.location = 'http://search.msn.com/results.aspx?q='+ escape(queryText + queryAugment) + ' {frsh=15}';
	}
	if(mode == "frsh" && direction == 0) {
		document.location = 'http://search.msn.com/results.aspx?q='+ escape(queryText + queryAugment);
	}
	if(mode == "frsh" && direction == 1) {
		document.location = 'http://search.msn.com/results.aspx?q='+ escape(queryText + queryAugment) + ' {frsh=85}';
	}
	
		// POPL
	if(mode == "popl" && direction == -1) {
		document.location = 'http://search.msn.com/results.aspx?q='+ escape(queryText + queryAugment) + ' {popl=15}';
	}
	if(mode == "popl" && direction == 0) {
		document.location = 'http://search.msn.com/results.aspx?q='+ escape(queryText + queryAugment);
	}
	if(mode == "popl" && direction == 1) {
		document.location = 'http://search.msn.com/results.aspx?q='+ escape(queryText + queryAugment) + ' {popl=85}';
	}
	
		// MTCH
	if(mode == "mtch" && direction == -1) {
		document.location = 'http://search.msn.com/results.aspx?q='+ escape(queryText + queryAugment) + ' {mtch=15}';
	}
	if(mode == "mtch" && direction == 0) {
		document.location = 'http://search.msn.com/results.aspx?q='+ escape(queryText + queryAugment);
	}
	if(mode == "mtch" && direction == 1) {
		document.location = 'http://search.msn.com/results.aspx?q='+ escape(queryText + queryAugment) + ' {mtch=85}';
	}
}

window.doMode = doMode;
window.parseParams = parseParams;


// Layout base system
var container = document.createElement("span");
var imgServer = 'http://127.0.0.1/pdc/';
var blackLeft = 'http://groups.msn.com/_Secure/0QQCVAsUTQ48JzN0rTLn7Ihe*BBusIWTekZqDfmwSAzEXy0t7TyhZzlw4kcvZndg95A6zAGV0zU67ycSrSYQWsy3PaJju91Wgdx1NAR*4ra0/blackLeft.png?dc=4675539294877614847';
var blackRight = 'http://groups.msn.com/_Secure/0QgDrAjgUHqy6*AZ9ge5W5jBqwcz9T8ErKxOiH3HwsCyi5BFMXw1rpJZwHLUAe8WFUdX6Lc!Am2DdIYTyi9Vd!DsIcg52AldwTzMKjh0v*o4/blackRight.png?dc=4675539294879110132';
var greenRight = 'http://groups.msn.com/_Secure/0QgCnAkwU*rDgvQDbX7LhGWkbNz6fYsJziuDZECXH3oSNcG90H0xIVU1QSy!jlOMBb4suV4SOUDy*je!bQ0px89kPVbnLA0HaVlUOMom5jWo/greenRight.png?dc=4675539294883484636';
var greenLeft = 'http://groups.msn.com/_Secure/0QQCVAtkTD5RxbNgBFtqrvJJMC*B5enZCMvNL8O!pyHF*2OFT6mCYg4hZFkl16Q7Rn2xxKuJKBtff22Ygj6aFTAoYBhLAZAbJzkYgqA4wgRM/greenLeft.png?dc=4675539294882291589';
var blogButton = 'http://groups.msn.com/_Secure/0QwDrAtAUG9LVuUeyXWTtbOcGr7mxh3dCwMEtHqCuPOe8mQ1ebjryphs!2!SNcQZINWDTTfHXEd0TTXG35zAKXex31esIJ9UW2BudOeqd8aE/blogsButton.png?dc=4675539294881246480';
var msdnButton = 'http://groups.msn.com/_Secure/0QgAZA2sUBrgDVoKRi9eepSDPRZzT0EuazjjJgyWsbWJQpVUbUNjV8O4fu5U!*9nEjdV9XTtMkBHHkOxgqPE810TGSGyv8QwTATO16pYfViI/msdnButton.png?dc=4675539294885372831';
var msdnBlue = 'http://groups.msn.com/_Secure/0QAAZA3cTcHtj58pAtNopLdvIhveAhiMTphdARM41yofeHouj4gNczQ9USId0YWoKQByGjlB3b7ObWwh1anWkAZYei*ys6YJgzowZAwBwBRg/msdnBlue.png?dc=4675539294884375444';
var blogsBlue = 'http://groups.msn.com/_Secure/0QQDrAtwTu5TVuUeyXWTtbBzwunO51S5VtGzNvdE!Ay95xJd2ykH8cIPiNSPMKYsGsJwfUTZG2qKXfKYGViBa12vZ!hoO5j7z19jhNJ57DrY/blogsBlue.png?dc=4675539294880080476';

var pdcLogo = 'http://groups.msn.com/_Secure/0QAAZA4QTK36R0soh0ZhJXK0dJWon9*JRgWytHHhQD7Vqq*lovZfqQyIvIcGhNbD*C5M5AmarhNsyfNxgy9IZIKoFtZagRvd*zowZAwBwZRc/pdc_logo.png?dc=4675539294886740857';
var rootTable = '<br clear="all"/><img src="'+pdcLogo+ '" height=46 align="left" hspace="8" vspace="2" style="padding:0px 15px 0px 0px"><table width="95%"><tr><th>Fresh</th><th>Popular</th><th>Precise</th><th>Only</th></tr><tr>';
rootTable += '<td id="radioFresh"><img src="'+greenLeft+'" hspace=0 vspace=0 onclick="doMode(\'frsh\',-1);">'
	+ '<img src="' + blackRight + '" hspace=0 vspace=0 ><img src="' + greenRight + '" hspace=0 vspace=0 onclick="doMode(\'frsh\',1);"></td>';

rootTable += '<td id="radioPopular"><img src="' + greenLeft + '" hspace=0 vspace=0  onclick="doMode(\'popl\',-1);">'
	+ '<img src="' + blackRight + '" hspace=0 vspace=0><img src="' + greenRight + '" hspace=0 vspace=0  onclick="doMode(\'popl\',1);"></td>';

rootTable += '<td id="radioPrecise"><img src="' + greenLeft + '" hspace=0 vspace=0  onclick="doMode(\'mtch\',-1);">'
	+ '<img src="' + blackRight + '" hspace=0 vspace=0><img src="' + greenRight +'" hspace=0 vspace=0  onclick="doMode(\'mtch\',1);"></td>';

rootTable += '<td id="onlyBlogs"><img src="'+blogButton+'" id="blogButton" onclick="doMode(\'blog\');"></td>';
rootTable += '<td id="onlyMsdn"><img src="'+msdnButton+'" id="msdnButton" onclick="doMode(\'msdn\');"></td>';
rootTable += '</tr></table><br clear="all">';



parseParams();

// Attach User Interface
container.innerHTML = rootTable;
document.getElementById("content").insertBefore( container,document.getElementById("header"));

// Set up UI state
//alert(' setting state for ' + popularity + ' ' +  precision + ' ' + freshness);

if(freshness != false) {
	if(freshness > 60) {
		//alert('frsh high');
		document.getElementById("radioFresh").innerHTML = '<img src="' + greenLeft + '" hspace=0 vspace=0'
		+ 'onclick="doMode(\'frsh\',-1);"><img src="' + greenLeft + '" hspace=0 vspace=0 onclick="doMode(\'frsh\',0);">'
		+ '<img src="' + blackRight + '" hspace=0 vspace=0>';
	}
	if(freshness < 40) {
		document.getElementById("radioFresh").innerHTML = '<img src="'+blackLeft +'" hspace=0 vspace=0 >' +
		'<img src="'+ greenLeft + '" hspace=0 vspace=0 onclick="doMode(\'frsh\',0);">'+
		'<img src="'+ greenRight + '" hspace=0 vspace=0 onclick="doMode(\'frsh\',1);">';
	}

}
if(popularity != false) {
	if(popularity > 60) {
		//alert('frsh high');
		document.getElementById("radioPopular").innerHTML = '<img src='+greenLeft + '" hspace=0 vspace=0'
		+ 'onclick="doMode(\'popl\',-1);"><img src="'+greenLeft + '" hspace=0 vspace=0 onclick="doMode(\'popl\',0);">'
		+ '<img src="'+blackRight + '" hspace=0 vspace=0>';
	}
	if(popularity < 40) {
		document.getElementById("radioPopular").innerHTML = '<img src='+blackLeft + '" hspace=0 vspace=0 >' +
		'<img src="'+greenLeft + '" hspace=0 vspace=0 onclick="doMode(\'popl\',0);">'+
		'<img src="'+greenRight + '" hspace=0 vspace=0 onclick="doMode(\'popl\',1);">';
	}

}
if(precision != false) {
	if(precision > 60) {
		//alert('prec high');
		document.getElementById("radioPrecise").innerHTML = '<img src="' + greenLeft + '" hspace=0 vspace=0'
		+ 'onclick="doMode(\'mtch\',-1);"><img src="'+ greenLeft + '" hspace=0 vspace=0 onclick="doMode(\'mtch\',0);">'
		+ '<img src="' + blackRight + '" hspace=0 vspace=0>';
	}
	if(precision < 40) {
		document.getElementById("radioPrecise").innerHTML = '<img src="' + blackLeft + '" hspace=0 vspace=0 >' +
		'<img src="' + greenLeft + '" hspace=0 vspace=0 onclick="doMode(\'mtch\',0);">'+
		'<img src="' + greenRight + '" hspace=0 vspace=0 onclick="doMode(\'mtch\',1);">';
	}

}

if(blogOnly) {
	document.getElementById("blogButton").src=blogsBlue;
}

if(msdnOnly) {
	document.getElementById("msdnButton").src=msdnBlue;
}





})();
