/* Copyright 2009 Trevor F. Smith and Clark Fredricksen */

var mousePosition = { x:0, y:0 };

function isAcceptibleBrowser(){
	if(jQuery.browser.msie == true && jQuery.browser.version < 7){
		return false;
	}
	return true;
}

function markAsNewsToMe(id){
	$.ajax({ type: "POST", url: "/stream/?link=" + id, async: false }).responseText;
	window.document.location.reload();
}

function registerLinkClick(id){
	$.ajax({ type: "GET", url: "/link/" + id + "/click/", async: false }).responseText;
}

function baseInit(){
   	$().mousemove(function(e){ mousePosition.x = e.pageX; mousePosition.y = e.pageY; });
	if(isAcceptibleBrowser() == false && document.location.href.indexOf('/update-your-browser/') == -1){
		document.location.href = '/update-your-browser/';
	} 
}
$(document).ready(function() { baseInit(); });

function scrollNewsStrip(id, goLeft, delta, duration){
	if(goLeft){
		distance = '+=' + delta + 'px';
	} else {
		distance = '-=' + delta + 'px';
	}
	$('#scrolling-news-strip-' + id).scrollTo( distance, { axis:'x', duration:duration } )
}

function playEmbed(linkID, embedID){
	$("#embed-popup").remove(); //just in case
	$("#content").append('<div id="embed-popup"></div>');
	$("#embed-popup").css('left', mousePosition.x - 300);
	$("#embed-popup").css('top', mousePosition.y - 200);
	$("#embed-popup").html('<div style="float: right">[<a href="." onclick="hideEmbed(); return false;">close</a>]</div>');
	$("#embed-popup").append('<iframe src="/embed/' + linkID + '/" width="600" height="400" ></iframe>');
}
function hideEmbed(){
	$("#embed-popup").html('ahoi');
	$("#embed-popup").remove();
}
function executeRemoteScript(url, callback){
	if(callback){
		if(url.indexOf('?') == -1)
			url = url + '?callback=' + callback;
		else
			url = url + '&callback=' + callback;
	}
	var obj=new JSONscriptRequest(url);     
	obj.buildScriptTag();
	obj.addScriptTag();
}

// JSONscriptRequest -- a simple class for accessing Yahoo! Web Services
// using dynamically generated script tags and JSON
// FOUND HERE: http://theurer.cc/code/jsonSamples/jsr_class.js
// Author: Jason Levitt
// Date: December 7th, 2005
//
// A SECURITY WARNING FROM DOUGLAS CROCKFORD:
// "The dynamic <script> tag hack suffers from a problem. It allows a page 
// to access data from any server in the web, which is really useful. 
// Unfortunately, the data is returned in the form of a script. That script 
// can deliver the data, but it runs with the same authority as scripts on 
// the base page, so it is able to steal cookies or misuse the authorization 
// of the user with the server. A rogue script can do destructive things to 
// the relationship between the user and the base server."
//
// So, be extremely cautious in your use of this script.
//

// Constructor -- pass a REST request URL to the constructor
//
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}
