var firstclick = 0;
var v3_firstclick = 0;
$(document).ready(function() {
	$("#register_icon").colorbox( {
		iframe : true,
		opacity : 0.7,
		scrolling : false,
		overlayClose : false,
		width : "600px",
		height : "460px"
	});
	
	$(".facebook_prompt").colorbox( {
		inline: true,
		opacity : 0.7,
		scrolling : false,
		overlayClose : false,
		width : "280px",
		height : "110px",
		href:".button_login_facebook"	
	});
	
	$("#login_icon").colorbox( {
		iframe : true,
		opacity : 0.7,
		scrolling : false,
		overlayClose : false,
		width : "280px",
		height : "310px"
	});
	$("#add_review").colorbox( {
		iframe : true,
		opacity : 0.7,
		scrolling : false,
		overlayClose : false,
		width : "320px",
		height : "500px"
	});
	$("#add_file").colorbox( {
		iframe : true,
		opacity : 0.7,
		scrolling : false,
		overlayClose : false,
		width : "450px",
		height : "480px"
	});
	$("#add_textbook").colorbox( {
		iframe : true,
		opacity : 0.7,
		scrolling : false,
		overlayClose : false,
		width : "687px",
		height : "480px"
	});
	$("#add_professor").colorbox( {
		iframe : true,
		opacity : 0.7,
		scrolling : false,
		overlayClose : false,
		width : "380px",
		height : "480px"
	});
	$("#rate_professor").colorbox( {
		iframe : true,
		opacity : 0.7,
		scrolling : false,
		overlayClose : false,
		width : "480px",
		height : "550px"
	});

	$(".dropdownmenu").click(function(e) {
		e.preventDefault();
		$("#signin_menu").slideToggle();
		$(".signin").toggleClass("menu-open");
		$("#signin_menu").delay(5000).slideUp();
	});
	$(".search").focusout(function() {
		if ($(".search").val() == '') {
			firstclick = 0;
			$(".search").val('search for modules or professors');
		}
		$("#results_display").delay(200).hide(1);
		return false;
	});
	$(".v3_search").focusout(function() {
		var isbn = $(".v3_search").val();
		isbn = jQuery.trim(isbn);
		isbn = str_replace(isbn, '-', '');
		if (!checkisbn(isbn)) {
			if ($(".v3_search").val() == '') {
				v3_firstclick = 0;
				$(".v3_search").val('modules, textbooks, professors');
			}
			$("#v3_results_display").delay(200).hide(1);
			return false;
		}
	});
	$(".addModule").focusout(function() {
		if ($(".search").val() == '') {
			firstclick = 0;
			$(".search").val('type to search');
		}
		$("#timetable_results").delay(200).hide(1);
		return false;
	});
	$(".tagmodule").focusout(function() {
		if ($(".tagmodule").val() == '') {
			firstclick = 0;
			$(".tagmodule").val('search for modules to tag');
		}
		$("#modules_results").delay(200).hide(1);
		return false;
	});
	$("#schocial_share_textbox").focusout(function() {
		if ($("#schocial_share_textbox").val() == '') {
			$("#schocial_share_textbox").val('What ideas have you?');
		}
		return false;
	});
	$("#schocial_share_textbox").focus(function() {
		if ($("#schocial_share_textbox").val() == 'What ideas have you?') {
			$("#schocial_share_textbox").val('');
		}
		return false;
	});
	$(".email_add").focusout(function() {
		if ($(".email_add").val() == '') {
			$(".email_add").val('email address');
		}
		return false;
	});
	$(".email_add").focus(function() {
		if ($(".email_add").val() == 'email address') {
			$(".email_add").val('');
		}
		return false;
	});
	
	$("#reset_school").click(function() {
		$.cookie("school", "0", {
			path : '/'
		});
		window.location = "/"
	});
	
	$("#nus").click(function() {
		$('#nus').addClass('on');
		$('#ntu').removeClass('on');
		$('#smu').removeClass('on');
		$('#schselector').slideUp('slow');
		$.cookie("school", "2", {
			path : '/'
		});
		$('#mascott').removeClass('mascott_ntu');
		$('#mascott').removeClass('mascott_smu');
		$('#mascott').css('background-position', '-150 0px');
		window.setTimeout(function() {
			self.parent.location.reload(); // "";
			}, 1000);

	});
	$("#ntu").click(function() {
		$('#ntu').addClass('on');
		$('#nus').removeClass('on');
		$('#smu').removeClass('on');
		$('#schselector').slideUp('slow');
		$.cookie("school", "1", {
			path : '/'
		});
		$('#mascott').removeClass('mascott_nus');
		$('#mascott').removeClass('mascott_smu');
		$('#mascott').css('background-position', '-300 0px');
		window.setTimeout(function() {
			self.parent.location.reload(); // "";
			}, 1000);
	});
	$("#smu").click(function() {
		$('#smu').addClass('on');
		$('#nus').removeClass('on');
		$('#ntu').removeClass('on');
		$('#schselector').slideUp('slow');
		$.cookie("school", "3", {
			path : '/'
		});
		$('#mascott').removeClass('mascott_nus');
		$('#mascott').removeClass('mascott_ntu');
		$('#mascott').css('background-position', '-300 0px');
		window.setTimeout(function() {
			self.parent.location.reload(); // "";
			}, 1000);
	});
	
	$("#schdropdownicon").click(function() {
		if ($('#schselector').is(':visible')) {
			$('#schselector').slideUp('slow');
		} else {
			$('#schselector').slideDown('slow');
		}
	});

	$(".buyprice,.sellprice,.spotprice").click(function(e) {
		if ($(this).val() == '0.00') {
			$(this).val('');
		}
	});
	
	$(".buyprice,.sellprice,.spotprice").focusout(function() {
		if ($(this).val() == '') {
			$(this).val('0.00');
		}
		else {
			// Check if it's too large or negative
			if ($(this).val() < 0 || isNumeric($(this).val()) == false)
			{
				alert("Invalid price. Please re-enter.");
				$(this).val('0.00');
			}
			else if ($(this).val() > 1000)
			{
				alert("Price cannot be more than $1000");
				$(this).val('0.00');
			}
			else {
				// Pass, round to two digit
				$(this).val(round($(this).val()));
			}
		}
		return false;
	});
});

function round(n) {
	return Math.round(n*100+((n*1000)%10>4?1:0))/100;
}

function parseScript(_source) {
	var source = _source;
	var scripts = new Array();
	
	// Strip out tags
	while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
		var s = source.indexOf("<script");
		var s_e = source.indexOf(">", s);
		var e = source.indexOf("</script", s);
		var e_e = source.indexOf(">", e);
		
		// Add to scripts array
		scripts.push(source.substring(s_e+1, e));
		// Strip from source
		source = source.substring(0, s) + source.substring(e_e+1);
	}
	
	// Loop through every script collected and eval it
	for(var i=0; i<scripts.length; i++) {
		try {
			eval(scripts[i]);
		}
		catch(ex) {
			// do what you want here when a script fails
		}
	}
	
	// Return the cleaned source
	return source;
}

function show_comment_box(type, id) {
	$('#wall_plug_' + type + '_' + id).fadeIn('slow');
	$('#new_comment_' + type + '_' + id).fadeIn('slow');
	$('#new_wall_post_' + type + '_' + id).focus();
}

function login_request(type, id) {
	$.colorbox( {
		inline: true,
		opacity : 0.7,
		scrolling : false,
		overlayClose : false,
		width : "280px",
		height : "110px",
		href:".button_login_facebook"	
	})
}

function isNumeric(value) {
	if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/))
		return false;
	return true;
}

function slideSwitch() {
	var $active = $('#slideshow IMG.active');

	if ($active.length == 0)
		$active = $('#slideshow IMG:last');

	var $next = $active.next().length ? $active.next()
			: $('#slideshow IMG:first');

	$active.addClass('last-active');

	$next.css( {
		opacity : 0.0
	}).addClass('active').animate( {
		opacity : 1.0
	}, 1000, function() {
		$active.removeClass('active last-active');
	});
}

$(function() {
	setInterval("slideSwitch()", 10000);
});

function toggle_schselect() {
	if ($('.ardora_schselector').is(':visible')) {
		$('.ardora_schselector').slideUp('slow');
	} else {
		$('.ardora_schselector').slideDown('slow');
	}
}

function toggle_modselect() {
	if ($('.ardora_modselector').is(':visible')) {
		$('.ardora_modselector').slideUp('slow');
	} else {
		$('.ardora_modselector').slideDown('slow');
	}
}

function str_replace(haystack, needle, replacement) {
	var temp = haystack.split(needle);
	return temp.join(replacement);
}

function checkisbn(isbnw) {
	
	  var isbn1 = isbnw.charAt(0);
	  var isbn2 = isbnw.charAt(1);
	  var isbn3 = isbnw.charAt(2);
	  var isbn4 = isbnw.charAt(3);
	  var isbn5 = isbnw.charAt(4);
	  var isbn6 = isbnw.charAt(5);
	  var isbn7 = isbnw.charAt(6);
	  var isbn8 = isbnw.charAt(7);
	  var isbn9 = isbnw.charAt(8);
	  var isbn10 = isbnw.charAt(9);

	  if (isbnw.length == 10) {
		  var val = eval(isbn1) * 10;
		  val = val + eval(isbn2) * 9;
		  val = val + eval(isbn3) * 8;
		  val = val + eval(isbn4) * 7;
		  val = val + eval(isbn5) * 6;
		  val = val + eval(isbn6) * 5;
		  val = val + eval(isbn7) * 4;
		  val = val + eval(isbn8) * 3;
		  val = val + eval(isbn9) * 2;	
	      val = val + eval(isbn10);
	
	      if ((val % 11) == 0) {
	          return true;
	      }
	      else {
	          return false;
	      }
	  }
	  else if (isbnw.length == 13) {
		  
		  var isbn11 = isbnw.charAt(10);
		  var isbn12 = isbnw.charAt(11);
		  var isbn13 = isbnw.charAt(12);

		  var val = eval(isbn1) * 1;
		  val = val + eval(isbn2) * 3;
		  val = val + eval(isbn3) * 1;
		  val = val + eval(isbn4) * 3;
		  val = val + eval(isbn5) * 1;
		  val = val + eval(isbn6) * 3;
		  val = val + eval(isbn7) * 1;
		  val = val + eval(isbn8) * 3;
		  val = val + eval(isbn9) * 1;	
	      val = val + eval(isbn10) * 3;	  
	      val = val + eval(isbn11) * 1;	 
	      val = val + eval(isbn12) * 3;	 

	  	  var remainder = val % 10;
	  	  var checkdigit = 10 - remainder;

	  	  if (checkdigit == 10) { 
	  		  checkdigit = 0; 
	  	  }

	  	  if (checkdigit == isbn13) {
		  	  return true;
	  	  }
	  	  else {
		  	  return false;
	  	  }
	  }
	  else {
		  return false;
	  }
}

function str_replace(haystack, needle, replacement) {
	var temp = haystack.split(needle);
	return temp.join(replacement);
}
