// JavaScript Document
///////////////////////////////////////////////////////////////////////////////// START
$(document).ready(function(){
/////////////////////////////////////////// LAUNCH INTERFACE

//setup slide navigation
$("#stage_prev").hide();
$("#stage_next").hide();
$(".stage_nextHold").show();
$(".stage_prevHold").show();
var contentStart = 1;
var holdContent_nav = "";

//setup nav links
$(".revimage_nav_entry").click(function() {
	if (holdContent_nav != "") {
	$("#"+holdContent_nav).show();
	$("#"+holdContent_nav+"_set").hide();
	}
	var checkContent_nav = $(this).attr("id");
	var checkContent_link = $(this).attr("link");
	var checkContent_id = $(this).attr("data");
	var checkContent_slides = $(this).attr("slides");
	$("#"+checkContent_nav).hide();
	$("#"+checkContent_nav+"_set").show();
	holdContent_nav = checkContent_nav;
	launcher_content(checkContent_id, checkContent_slides, contentStart, checkContent_link);
});

//AUTO load products
var checkContent_nav = "nav_products";
var checkContent_link = "function_products.php";
var checkContent_id = "1";
var checkContent_slides = "1";
$("#"+checkContent_nav).hide();
$("#"+checkContent_nav+"_set").show();
holdContent_nav = checkContent_nav;
launcher_content(checkContent_id, checkContent_slides, contentStart, checkContent_link);
//END AUTO load products

//setup video player link
$("#videoLink").click(function() {
	if (holdContent_nav != "") {
	$("#"+holdContent_nav).show();
	$("#"+holdContent_nav+"_set").hide();
	resetStage();
	}
	$("#mainStage").empty();
	$.ajax({
  	url: "function_video.php",
  	cache: false,
  	success: function(incomingContent){
	$("#mainStage").append(incomingContent);
	embedPlayer();
	flashInteract_send_video($(this).attr("videoNumber"));
  	}
	});
});

$("#stage_next").delay(4000).click();

////// END DOC
});

//////////////////////// THE REVIMAGE FUNCTIONS
var resetStage = function () {
$("#stage_prev").hide();
$("#stage_next").hide();
$(".stage_nextHold").show();
$(".stage_prevHold").show();
contentStart = 1;
holdContent_nav = "";
}

var launcher_content = function(checkContent_id, checkContent_slides, contentStart, checkContent_link) {
	resetStage();
	$("#mainStage").empty();
	if (checkContent_slides == 1){
	$.ajax({
  	url: "function_slides.php?category="+checkContent_id+"&start="+contentStart,
  	cache: false,
  	success: function(incomingContent){
	$("#mainStage").empty();
	$("#mainStage").append(incomingContent);
	$("#slideImage").css('opacity', '0.0');
	$("#slideImage").animate({opacity: "1"}, 900);
	$(".stage_nextHold").hide();
	$("#stage_next").show();
	setup_slideNav(checkContent_id, contentStart);
  	}
	});
	} else {
	$.ajax({
  	url: checkContent_link,
  	cache: false,
  	success: function(incomingContent){
	$("#mainStage").empty();
	$("#mainStage").append(incomingContent);
	if (checkContent_link == "function_contact.php"){
	setupFormFields();
	}
  	}
	});
	}
}
var setup_slideNav = function(checkContent_id, contentStart) {
	$("#stage_next").click(function() {
		var marginDirection = "Left";
		contentStart++;
		loadSlide(checkContent_id, contentStart, marginDirection);
	});
	$("#stage_prev").click(function() {
		var marginDirection = "Right";
		contentStart--;
		loadSlide(checkContent_id, contentStart, marginDirection);
	});
}

var loadSlide = function(checkContent_id, contentStart, marginDirection) {
$checkSlideTotal = $("#slideImage").attr("total");
if (contentStart == 1){
		$("#stage_prev").hide();
		$(".stage_prevHold").show();
		} else {
		$("#stage_prev").show();
		$(".stage_prevHold").hide();
		}
		if (contentStart == $checkSlideTotal){
			$("#stage_next").hide();
			$(".stage_nextHold").show();
		} else {
			$("#stage_next").show();
			$(".stage_nextHold").hide();
		}
if (marginDirection == "Left") {
	$("#slideImage").animate({opacity: "0.0", marginLeft: "-880px"}, 600,
		function () {
		if (contentStart <= $checkSlideTotal){
			$.ajax({
  			url: "function_slides.php?category="+checkContent_id+"&start="+contentStart,
  			cache: false,
  			success: function(incomingContent){
			$("#mainStage").empty();
    		$("#mainStage").append(incomingContent);
			$("#slideImage").css({opacity: '0.0', marginLeft: "880px"}); 
			$("#slideImage").delay(600).animate({opacity: "1", marginLeft: "0px"}, 620);
  			}
			});
		}
		});
} else {
	$("#slideImage").animate({opacity: "0.0", marginLeft: "880px"}, 600,
		function () {
		if (contentStart <= $checkSlideTotal){
			$.ajax({
  			url: "function_slides.php?category="+checkContent_id+"&start="+contentStart,
  			cache: false,
  			success: function(incomingContent){
			$("#mainStage").empty();
    		$("#mainStage").append(incomingContent);
			$("#slideImage").css('opacity', '0.0');
			$("#slideImage").css({opacity: '0.0', marginLeft: "-880px"}); 
			$("#slideImage").delay(600).animate({opacity: "1", marginLeft: "0px"}, 620);
  			}
			});
		}
		});
}
}

/////////////////////////////////////////// EMBED PLAYER
var embedPlayer = function () {
var flashvars = {
  magVar_null: "var_null"
};
var params = {
  allowScriptAccess: "sameDomain",
  menu: "false",
  wmode: "opaque",
  allowFullScreen: "true",
  quality: "high",
  id: "magneticPlayer",
  name: "magneticPlayer",
  src: "magneticPlayer.swf",
  movie: "magneticPlayer.swf"
};
var attributes = {
  id: "magneticPlayer",
  name: "magneticPlayer"
};
swfobject.embedSWF("magneticPlayer.swf", "videoContainer", "800", "450", "9.0.0","", flashvars, params, attributes);
}

///////////////////////////////////////////// FORMS

var echeck = function (str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){   
		    return false
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){    
		    return false
		 }
		 if (str.indexOf(dot,(lat+2))==-1){ 
		    return false
		 }
		 if (str.indexOf(" ")!=-1){ 
		    return false
		 }
 		 return true					
}
var revContactSubmit = function (form) {
var getFullName = form.revContact_fullname.value;
var getEmail = form.revContact_email.value;
var getInfo = form.revContact_info.value;
var getCompany = form.revContact_company.value;
var getPhone = form.revContact_phone.value;
var getReceive = form.revContact_receive.checked;
if (getReceive == false) {
getReceive = "No";
} else {
getReceive = "Yes";
}
if (getInfo == "Questions or Comments (optional)") {
getInfo = "None";
}
  if(getFullName=="" || getFullName=="Full Name" || getEmail=="" || getInfo=="") {
	////// VALIDATE ERROR
	alert('Please complete the form. (Company and Phone are optional)');
   return false;
  }
  var emailID=getEmail;
	if ((emailID==null)||(emailID=="")){
		alert("Please enter a valid email address.");
		return false
	}
	if (echeck(emailID)==false){
		alert("Please enter a valid email address.");
		return false
	}
 // VALIDATED - SEND
 	$.ajax({
  			url: "function_contact_receive.php?fullname="+getFullName+"&email="+getEmail+"&message="+getInfo+"&company="+getCompany+"&phone="+getPhone+"&receive="+getReceive,
  			cache: false,
  			success: function(incomingContent){
			$("#mainStage").empty();
    		$("#mainStage").append(incomingContent);
  			}
			});
  return false;
}

var setupFormFields = function () {
var revData_fullName = $("#revContact_fullname").val();
var revData_email = $("#revContact_email").val();
var revData_info = $("#revContact_info").val();
var revData_company = $("#revContact_company").val();
var revData_phone = $("#revContact_phone").val();
$("#revContact_fullname").focus(function() {
$(this).val(""); }).blur(function() { if ($(this).val() == "") { $(this).val(revData_fullName);}});
$("#revContact_email").focus(function() {
$(this).val(""); }).blur(function() { if ($(this).val() == "") { $(this).val(revData_email);}});
$("#revContact_info").focus(function() {
$(this).val(""); }).blur(function() { if ($(this).val() == "") { $(this).val(revData_info);}});
$("#revContact_company").focus(function() {
$(this).val(""); }).blur(function() { if ($(this).val() == "") { $(this).val(revData_company);}});
$("#revContact_phone").focus(function() {
$(this).val(""); }).blur(function() { if ($(this).val() == "") { $(this).val(revData_phone);}});
}



///////////////////////////////////////////// FLASH ACTIONSCRIPT GATEWAY

/// SEND INTO FLASH
function identifyFlashTarget(movieName) {
  var isIE = navigator.appName.indexOf('Microsoft') != -1;
  return (isIE) ? window[movieName] : document[movieName];
};
function flashInteract_send_video(varData) {
  identifyFlashTarget("magneticPlayer").flashConn_send_video(varData);
}
function flashInteract_send_watermark(varData) {
  identifyFlashTarget("magneticPlayer").flashConn_send_watermark(varData);
}
function flashInteract_send_videoCtrl(varData) {
  identifyFlashTarget("magneticPlayer").flashConn_send_videoCtrl(varData);
}

/// RECEIVE FROM FLASH
function flashInteract_receive_video(varData) {
	resetInformationLoader(varData);
}
function flashInteract_adv_launch(varData) {
	ad_interface_open(varData);
}
function flashInteract_alert(varData) {
	alert(varData);
}