﻿function changeFontSize( size )
{    
    if( size == 'normal' )
    {
        document.body.style.fontSize = '83%';
        eraseCookie( 'fontSizeF' );
        createCookie( 'fontSizeF', 'normal', 1 );        
    }
    else if( size == 'large' )
    {
        document.body.style.fontSize = '100%';
        eraseCookie( 'fontSizeF' );
        createCookie( 'fontSizeF', 'large', 1 );
    }
    else if( size == 'largest' )
    {
        document.body.style.fontSize = '120%';
        eraseCookie( 'fontSizeF' );
        createCookie( 'fontSizeF', 'largest', 1 );
    }
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}







/* grab Elements from the DOM by className */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* toggle an element's display */
function toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

/* check/uncheck all checkboxes */
function checkAll(ref) {
  var chkAll = document.getElementById('checkAll');
  var checks = document.getElementsByName('del[]');
  var removeButton = document.getElementById('removeChecked');
  var boxLength = checks.length;
  var allChecked = false;
  var totalChecked = 0;
  if ( ref == 1 ) {
    if ( chkAll.checked == true ) {
      for ( i=0; i < boxLength; i++ ) {
        checks[i].checked = true;
      }
    }
    else {
      for ( i=0; i < boxLength; i++ ) {
        checks[i].checked = false;
      }
    }
  }
  else {
    for ( i=0; i < boxLength; i++ ) {
      if ( checks[i].checked == true ) {
        allChecked = true;
        continue;
      }
      else {
        allChecked = false;
        break;
      }
    }
    if ( allChecked == true ) {
      chkAll.checked = true;
    }
    else {
      chkAll.checked = false;
    }
  }
  for ( j=0; j < boxLength; j++ ) {
    if ( checks[j].checked == true ) {
      totalChecked++;
	}
  }
  removeButton.value = "Remove ["+totalChecked+"] Selected";
}


/* popup in new window of img size */
function PopupPic(sPicURL, height, width ) { 
	window.open("Popup/imagePopup.html?"+sPicURL, "", "resizable=1,HEIGHT="+ height +",WIDTH=" + width );
}


//-------------------------------------------------------------------------------------------------------------------------------------


// Dressup finder functions

function OnFlashGameInitalize(flash, height, width) {
	var code =	'<object width="' + width + '" height="' + height + '">' +
				'	<param name="movie" value="' + flash + '"></param>' +
				'	<param name="allowFullScreen" value="true"></param>' +
				'	<param name="allowscriptaccess" value="always"></param>' +
				'	<embed src="' + flash + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + width + '" height="' + height + '"></embed>' +
				'</object>';
	$("#swfgame").html(code);
}
/*-----------------*/
// Updates the last played and hit count status for a game
function AddGameHit(gameID) {
	Dressup.Services.Game.UpdateGameHitCount(gameID, GameHitComplete, GameHitFailed);
}
function GameHitComplete(result, panel, methodName) {	
}
function GameHitFailed(exception, panel, methodName) {
	alert("Couldn't add game hit");
}
/*-----------------*/
function InsertRating(gameID, rating) {
	//Check if user has rated this game before
	var alreadyRated = false;
	if (!alreadyRated) {
		Dressup.Services.Game.InsertGameRating(gameID, rating, GameRatingComplete, GameRatingFailed);
	}
	else {
		//Deactivate the ratings
		$('.page .rate .star-rating').rating('readOnly', true);
	}	
}
function GameRatingComplete(result, panel, methodName) {
	//Deactivate the ratings
	$('.page .rate .star-rating').rating('readOnly', true);
}
function GameRatingFailed(exception, panel, methodName) {
	alert("Couldn't add game rating");
}
/*-----------------*/

function SendGame(name,url,desc) {
	//Hide the notification if it is visible
	$(".side .SendGame .notification").hide();
	$(".side .SendGame div.spinner").show();
	$(".side .SendGame .controls").hide();
	Dressup.Services.Game.SendGame(name, url, desc, GameSendComplete, GameSendFailed);	
}
function GameSendComplete(result, panel, methodName) {	
	if( result == true )
	{
		//Clear the fields
		$(".side .SendGame .name input").val('');
		$(".side .SendGame .url input").val('');
		$(".side .SendGame .desc textarea").val('');
	
		//Display the success notification
		$(".side .SendGame .notification").html('<p class="success">Game sent, thank you.</p>');
		$(".side .SendGame .notification").slideDown("normal");
	}
	else
	{
		//Display the error notification
		$(".side .SendGame .notification").html('<p class="error">Game not sent, please try again later.</p>');
		$(".side .SendGame .notification").slideDown("normal");
	}
	
	//Hide the notification after 7000ms delay
    HideNotification(7000);
    
    $(".side .SendGame div.spinner").hide();
    $(".side .SendGame .controls").show();
}
function GameSendFailed(exception, panel, methodName) {
	//Display the error notification
	$(".side .SendGame .notification").html('<p class="error">Game not sent, please try again later.</p>');
	$(".side .SendGame .notification").slideDown("normal");
	 
	//Hide the notification after 7000ms delay
    HideNotification(7000);
    
    $(".side .SendGame div.spinner").hide();
    $(".side .SendGame .controls").show();
}
/*-----------------*/


//Post link to facebook
function fbs_click() {
	u = location.href;
	t = document.title;
	window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
	return false;
}
