/*!
 * $Id: common.js 2531 2010-08-17 18:41:34Z mloftis $
 *
 * Common javascript, eg page ready.
 * Must be loaded AFTER the jQuery modules.
 */
//Included since IE at least is massively retarded. 
//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}
 

var ratingStarsRunOn = [];

function getCookieVal(offset){
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == - 1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}


function GetCookie (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal (j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) {
            break;
        }
    }
    return null;
}
function SetCookie (name, value) {
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;

    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;

    cookiestr = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");

    document.cookie = cookiestr;
}

var ep_pageView = GetCookie("cm-exit-pop-view");
var ep_pop = GetCookie("cm-exit-pop-count");
if(ep_pageView == null) ep_pageView = 0; else ep_pageView = parseInt(ep_pageView);
if(ep_pop == null) ep_pop = 0; else ep_pop = parseInt(ep_pop);


/**
 * Adds the indicated bits to the stack of rating stars to be added
 * when the document indicates it's ready so it can call blah.rating
 * should be called from/via a $(document).ready()
 */

function ratingStars(options) {
	var raterOptions = {
		style: options.rating_style,
		curvalue: options.current_rating,
		maxvalue: options.max_rating,
		isStatic: options.rating_static,
		statid: $('#'+options.div_name+'res'),
		fbkid: $('#'+options.div_name+'fbk'),
		numvotes: options.numvotes
		
	};
	// check to see if we've already been active...
	if(ratingStarsRunOn.indexOf(options.div_name) == -1) {
	  ratingStarsRunOn.push(options.div_name);
	  $('div#'+options.div_name).sp3rater(options.rating_callback_url,raterOptions);
	}

	
}


var loadpieceInfo = {
  lasturl: null
  };

/**
 * Loads in a URL into a specified divName, and applies the function to
 * all the links inside the pagination div of that page (to preserve the ajax-request)
 * @param string href The URL of the page to load
 * @param string divName The name of the DOM-element to load the data into
 * @return boolean False To prevent the links from doing anything on their own.
 */
function loadPaginatedPiece(href,divName) {
    loadpieceInfo.lasturl = href;    
    $(divName).load(href, function(){
        var divPaginationLinks = divName+" a";
        /*$(divPaginationLinks).click(function() {     
            var thisHref = $(this).attr("href");
            loadPaginatedPiece(thisHref,divName);
            return false;
        });*/
        divPaginationLinks = divName+" a.clickclass";
        $(divPaginationLinks).click(function() {     
            var thisHref = $(this).attr("href");
            loadPaginatedPiece(thisHref,divName);
            return false;
        });
        
    });
} 

var THUMBS_HTTP_ROOT = "";
var thumb_t_ID=null;
var thumb_t_selected=null;

$(document).ready(function() {

	if(false) {
		$("div#homenews").corner("2px");
		$("div#homepics").corner("bottom 2px");
		$("div#homevideonew").corner("bottom 2px");
		$("div#video_meta").corner("bottom 2px");
		$("div.roundedbottom").corner("bottom 2px");
		$("div.roundedtop").corner("top 2px");
		$("div.rounded").corner("2px");

		$("h1.header").corner("top 2px");
		$("h1.related").corner("3px");
		$("h1.comments_full").corner("3px");
		$(".row_full_width_pics").corner("2px");
		$(".row").corner("bottom 2px");
		$("#member").corner("2px");
		$("#recommendedlinks").corner("bottom 2px");
	}

	$("#related_video .videolist:nth-child(4n)").addClass("last");  // Had to use this to trim margin from the last element.  Too tight for just CSS :)

	var ThumbsCacheStack = [];

	$.preLoadImages = function(images) {
		for (var i=0; i<images.length; i++){
			var cacheImage = document.createElement('img');
			cacheImage.src = images[i];
			ThumbsCacheStack.push(cacheImage);
		}
	}

	$.cicleThumbsfrom = function(ThumbImageObject) {
		thumb_t_selected++;
		if(thumb_t_selected >= ThumbImageObject.thumbs.length){
			thumb_t_selected = 1;
		}
		ThumbImageObject.attr("src",ThumbImageObject.thumbs[thumb_t_selected]);
	}

	try{
		$("img.rotate-thumbs").livequery("mouseenter",
	
			function(){
				clearInterval(thumb_t_ID);
				var self=$(this);
				var params = self.metadata();
				self.thumbs = new Array();
				//thumb_t_selected = self.metadata().ethumbs.indexOf(params.selected_thumb);
				i=0;
			
				for(thumb in params.ethumbs){
					self.thumbs[i] = THUMBS_HTTP_ROOT + params.ethumbs[thumb];
					i++;
				}
			
				$.preLoadImages(self.thumbs);
				
				$.cicleThumbsfrom(self);
				
				thumb_t_ID = setInterval(
					function(){
						$.cicleThumbsfrom(self);
					}
				,500);
			}
		);
	}
	catch(e){ 
		console.log(e.message); 
	}

	try{
		$("img.rotate-thumbs").livequery("mouseleave",
			function(){
				var self=$(this);
				var params = self.metadata();
				self.attr("src", THUMBS_HTTP_ROOT + params.selected_thumb);
				clearInterval(thumb_t_ID);
				thumb_t_selected = null;
			}
		);
	}
	catch(e){ 
		console.log(e.message); 
	}
}); // end document ready

