// JavaScript Document
///////////////////////////////////////////////////////////////////////////////// START
$(document).ready(function(){
/////////////////////////////////////////// LAUNCH INTERFACE
//LOOP
//var i=1;
//for (i=1;i<=2;i++)
//{}
/////// SETUP THE STAGE
//$("#headlocks_logo").show();
$("#headlocks_logo").hide();
var status_audio = 0;
//$("#navPagePrev").hide();
//$("#navPageNext").hide();
$("#tumblrPagination").css('opacity', 0);
$("#nav_link_home").css('opacity', 0);

$("#nav_link_home").click(function() {
	$("#headlockDesktop").css("background-image","url(files/backdrops/"+checkBackdrop+")");
	$("#tumblrPagination").css('opacity', 0);
	$(this).css('opacity', 0);
	$("#headlocks_logo").hide();
	launcher_index();
});

/////// CHECK FOR EXTERNAL LINK DATA
var checkExternal = $("body").attr("share");
var checkExternalType = $("body").attr("type");

/////// SET THE BACKGROUND IMAGE
//$("#headlocks_logo").hide();
var checkBackdrop = $("body").attr("backdrop");
$("#headlockDesktop").css("background-image","url(files/backdrops/"+checkBackdrop+")");

/////// IF EXTERNAL LINK LAUNCH LINK ELSE LAUNCH SITE
if (checkExternal!="none") {
launcher_external(checkExternalType, checkExternal);
} else {
//auto display dropdown open
$(".dropdown:eq(0)").slideDown(100);
launcher_index();
}
/////// MAIN NAV LINKS
$(".txt_menuItem").click(function() {
	var checkContent_id =$(this).attr("val");
	$("#tumblrPagination").css('opacity', 0);
	$("#nav_link_home").delay(900).queue(function () {$(this).css({'opacity': 1});$(this).dequeue();})
	launcher_content(checkContent_id, 0);
});
/////// FACEBOOK LINKS
$(".fb_logoutButton_index").click(function() {
	fb_logout("http://www.theheadlocks.com/web");
});

////// END DOC
});

////////////////////////////////////////// FACEBOOK FUNCTIONS

var onConnected = function(user_id) {
	$(".fb_disconnected").hide();
	$(".fb_connected").show();
} 
var onNotConnected = function() {
	$(".fb_disconnected").show();
	$(".fb_connected").hide();
}
var fb_logout = function(redirectURLdata) {
	FB.Connect.logoutAndRedirect(redirectURLdata);
}
//get uid FB.Connect.get_loggedInUser();

////////////////////////////////////////// HEADLOCKS INDEX PAGE SETUP

var launcher_index = function() {
$.ajax({
  url: "function_index.php",
  cache: false,
  success: function(incomingContent){
	$("#stage").empty();
    $("#stage").append(incomingContent);
	setup_index();
  }
});
}
var setup_index = function() {
	//trigger_indexVideo(); ACTIVATES VIDEO ON INDEX PAGE
	var checkAnnounce = $("body").attr("announce");
	if (checkAnnounce == "1") {
		trigger_announcement("1");
	}
	// TRIGGER SETUP FOR MAILING LIST
	setup_contactLink();
	
	//$(".index_viewLink").click(function() {
	//launcher_content("start", 0);
	//});
}

var trigger_indexVideo = function(videoData) {
	var flashvars = {};
	flashvars.allowFullScreen = true;
	var attribs = { id:"headlocks_indexVideo" };
	var params = {};
	params.bgcolor = "#000000";
	params.allowScriptAccess = "sameDomain";
	params.allowFullScreen = flashvars.allowFullScreen;
	params.wmode = "opaque";
	swfobject.embedSWF("index_video.swf", "indexVideo_container", "200", "200", "9.0.28", "/swfobject/expressInstall.swf", flashvars, params, attribs);
}

var trigger_announcement = function(announceData) {
$.ajax({
	  url: "function_announcement.php?id="+announceData,
	  cache: false,
	  success: function(incomingContent){
		$("#modalBox").empty();
    	$("#modalBox").append(incomingContent);
		$("#modalWrapper").show();
		$("#modalBoxExit").click(function() {
			$("#modalBox").empty();
			$("#modalWrapper").hide();
		});
	}
});
}

////////////////////////////////////////// HEADLOCKS START CONTENT SETUP
var launcher_content = function(contentType, contentStart) {
$("#headlocks_logo").show();
$("#tumblrPagination").animate({opacity: 0}, 300);
$("#stage").animate({ opacity: 0.0 }, 600, function() {
//var randomBackdrop=Math.floor(Math.random()*1);
var randomBackdrop=3;
$("#headlockDesktop").css("background-image","url(files/backdrops/"+randomBackdrop+".jpg)");
$("#stage").empty();
	$.ajax({
		url: "function_start.php?type="+contentType+"&start="+contentStart,
  		cache: false,
  		success: function(incomingContent){
    		$("#stage").append(incomingContent);
			$(".content_data").css("opacity","0");
			$(".content_entry").hide();
			setup_content(contentType);
		}
	});
});
}
var setup_content = function(contentType) {
	$(".content_entry").each(function(i) {
	var randomSlide=Math.floor(Math.random()*6);
	$(this).find(".content_data").css('background-image', 'url(polaroids/polaroid_'+randomSlide+'.jpg)');
    $(this).delay(i*150).show(150);
		$(this).hover(function() {
			var checkData =$(this).attr("data");
			$("#data_"+checkData).animate({opacity: 1}, 400 );
  			}, function() {
			var checkData =$(this).attr("data");
    		$("#data_"+checkData).animate({opacity: 0}, 400 );
		});
	});
	setup_audioLink();
	setup_photoLink();
	setup_videoLink();
	setup_regularLink();
	setup_quoteLink();
	
	setup_pagination(contentType);
}

var setup_audioLink = function() {
	$(".sprite_audio").click(function() {
	var checkContent_id =$(this).attr("data");
	var checkContent_type =$(this).attr("type");
	launcher_audio(checkContent_id, checkContent_type);
	
	$(".asset_bird").click(function() {
	$(".embed_audio").empty();
	$(".audioBox").hide();
	status_audio = 0;
	});
});
}
var setup_photoLink = function() {
	$(".sprite_photo").click(function() {
	var checkContent_id =$(this).attr("data");
	launcher_photo(checkContent_id, "local");
});
}
var setup_videoLink = function() {
	$(".sprite_video").click(function() {
	var checkContent_id =$(this).attr("data");
	launcher_video(checkContent_id, "local");
});
}
var setup_regularLink = function() {
	$(".sprite_regular").click(function() {
	var checkContent_id =$(this).attr("data");
	launcher_regular(checkContent_id, "local");
});
}
var setup_quoteLink = function() {
	$(".sprite_quote").click(function() {
	var checkContent_id =$(this).attr("data");
	launcher_quote(checkContent_id, "local");
});
}

var setup_pagination = function(contentType) {
$("#navPagePrev").unbind('click');
$("#navPageNext").unbind('click');
			var checkStartNum = Math.floor($("#pagesData").attr("pagestart"));
			var checkCurrentPage = Math.floor($("#pagesData").attr("pagecurrent"));
			var checkTotalPages = Math.floor($("#pagesData").attr("pagetotal"));
			$("#pageCurrent").text(checkCurrentPage);
			$("#pageTotal").text(checkTotalPages);
			var contentNext = Math.floor(checkStartNum + 18);
			var contentPrev = Math.floor(checkStartNum - 18);
			if (checkCurrentPage == 1 && checkCurrentPage < checkTotalPages) {
				//alert("pg 1 type: "+contentType+" - prev start: "+contentPrev+" - next start: "+contentNext);
				$("#navPagePrev").hide();
				$("#navPageNext").show();
				$("#stage").animate({opacity: 1}, 600, function () {			
				$("#tumblrPagination").animate({opacity: 1}, 600);
				});
				$("#navPageNext").click(function() {
					launcher_content(contentType, contentNext);
				});
				
			} else if (checkCurrentPage > 1 && checkCurrentPage < checkTotalPages) {
				//alert("pg 2 type: "+contentType+" - prev start: "+contentPrev+" - next start: "+contentNext);
				$("#navPagePrev").show();
				$("#navPageNext").show();
				$("#stage").animate({opacity: 1}, 600, function () {			
				$("#tumblrPagination").animate({opacity: 1}, 600);
				});
				$("#navPagePrev").click(function() {
					launcher_content(contentType, contentPrev);
				});
				$("#navPageNext").click(function() {
					launcher_content(contentType, contentNext);
				});
				
			} else if (checkCurrentPage > 1 && checkCurrentPage == checkTotalPages) {
				//alert("pg 3 type: "+contentType+" - prev start: "+contentPrev+" - next start: "+contentNext);
				$("#navPageNext").hide();
				$("#navPagePrev").show();
				$("#stage").animate({opacity: 1}, 600, function () {			
				$("#tumblrPagination").animate({opacity: 1}, 600);
				});
				$("#navPagePrev").click(function() {
					launcher_content(contentType, contentPrev);
				});
				
			} else if (checkCurrentPage == 1 && checkCurrentPage == checkTotalPages) {
				$("#navPageNext").hide();
				$("#navPagePrev").hide();
				$("#stage").animate({opacity: 1}, 600, function () {			
				//$("#tumblrPagination").animate({opacity: 1}, 600);
				});
			}
// END Setup Pagination
}

////////////////////////////////////////// HEADLOCKS EXTERNAL LINK SETUP
var launcher_external = function(contentType, idData) {
	if (contentType == "photo") {
	launcher_photo(idData, "external");
	} else if (contentType == "video") {
	launcher_video(idData, "external");
	}
}

////////////////////////////////////////// HEADLOCKS DETAIL VIEW LAUNCHERS
var launcher_audio = function(contentID) {
$.ajax({
  url: "function_audio.php?id="+contentID,
  cache: false,
  success: function(incomingContent){
	$(".embed_audioData").empty();
    $(".embed_audioData").append(incomingContent);
	$(".audioBox").show();
	$(".embed_audio embed").click();
	status_audio = 1;
  }
});
}
var launcher_video = function(contentID, externalCheck) {
  if (externalCheck == "local") {
  $.ajax({
  url: "function_video.php?id="+contentID,
  cache: false,
  success: function(incomingContent){
	$("#modalBox").empty();
    $("#modalBox").append(incomingContent);
	$("#modalWrapper").show();
	$("#modalBoxExit").click(function() {
		$("#modalBox").empty();
		$("#modalWrapper").hide();
	});
	if (status_audio == 1){
		$(".embed_audio").empty();
		$(".audioBox").hide();
		status_audio = 0;
	}
  }
  });
  } else {
	$.ajax({
  url: "function_video.php?id="+contentID+"&ext=1",
  cache: false,
  success: function(incomingContent){
	$("#stage").empty();
	//$("#modalBox").attr("ext", "1");
	$("#modalBox").empty();
    $("#modalBox").append(incomingContent);
	$("#modalWrapper").show();
	$("#modalBoxExit").click(function() {
		$("#modalBox").empty();
		$("#modalWrapper").hide();
	});

	if (status_audio == 1){
		$(".embed_audio").empty();
		$(".audioBox").hide();
		status_audio = 0;
	}
  }
  });
  }
}
var launcher_photo = function(contentID, externalCheck) {
	if (externalCheck == "local") {
  		$.ajax({
  		url: "function_photo.php?id="+contentID,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			//$("#modalBox").text("xx");
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("The Headlocks");
				});
				}
		});
	} else {
	  ///// THIS IS AN EXTERNAL LINK THRU
	  $.ajax({
  		url: "function_photo.php?id="+contentID,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("The Headlocks");
				});
				}
		});
  	}
}
var launcher_regular = function(contentID, externalCheck) {
	if (externalCheck == "local") {
  		$.ajax({
  		url: "function_regular.php?id="+contentID,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("The Headlocks");
				});
				}
		});
	} else {
	  ///// THIS IS AN EXTERNAL LINK THRU
	  $.ajax({
  		url: "function_regular.php?id="+contentID,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("The Headlocks");
				});
				}
		});
  	}
}
var launcher_quote = function(contentID, externalCheck) {
	if (externalCheck == "local") {
  		$.ajax({
  		url: "function_quote.php?id="+contentID,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("The Headlocks");
				});
				}
		});
	} else {
	  ///// THIS IS AN EXTERNAL LINK THRU
	  $.ajax({
  		url: "function_quote.php?id="+contentID,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("The Headlocks");
				});
				}
		});
  	}
}

////////////////////// INDEX PAGE MAILING LIST
var setup_contactLink = function() {
	$(".contactLink").click(function() {
	var checkListType =$(this).attr("data");
	launcher_contactSignup(checkListType);
});
}
var launcher_contactSignup = function(listType){
	getGoogleLocation = $('#location').attr('data');
		$.ajax({
  		url: "function_contactSignup.php?type="+listType+"&location="+getGoogleLocation,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			if (listType == "email") {
				setup_listEmail();
			} else if (listType == "txt") {
				setup_listTxt();
			}
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("The Headlocks");
				});
				}
		});
}

var setup_listTxt = function() {
	$(".listAlertText").css("display", "none");
	var txtList_location = $("#sigupSetup").attr("location");
	$("#listTxt_go").click(function() {
	var txtList_name = $("#txtList_name").val();
	var txtList_cellno = $("#txtList_cellno").val();
	var txtList_carrier = $("#txtList_carrier").val();
	if (txtList_name == '') {
		$("#txtList_name").css("border", "solid 2px #7e5728");
	} else {
		$("#txtList_name").css("border", "none");
	}
	if (txtList_cellno == '') {
		$("#txtList_cellno").css("border", "solid 2px #7e5728");
	} else {
		$("#txtList_cellno").css("border", "none");
	}
	if (txtList_carrier == 'null') {
		$("#txtList_carrier").css("border", "solid 2px #7e5728");
	} else {
		 $("#txtList_carrier").css("border", "none");
	}
	if (txtList_name == '' || txtList_cellno == '' || txtList_carrier == 'null') {
	$(".listAlertText").css("display", "block");
	} else {
		$(".listAlertText").css("display", "none");
		launcher_listTxt_verify(txtList_name, txtList_cellno, txtList_carrier, txtList_location);
	}
});
}
var setup_txtVerify = function() {
	$(".listAlertText_val").css("display", "none");
	var txtList_location = $("#signupData").attr("location");
	var txtList_name = $("#signupData").attr("name");
	var txtList_cellno = $("#signupData").attr("cellno");
	var txtList_carrier = $("#signupData").attr("carrier");
	var txtList_status = $("#signupData").attr("status");
	
	$("#listTxt_activate").click(function() {
	var txtList_key = $("#txtList_activation").val();
	if (txtList_key == '') {
		$(".listAlertText_val").css("display", "block");
		$("#txtList_activation").css("border", "solid 2px #7e5728");
	} else {
		$(".listAlertText_val").css("display", "none");
		$("#txtList_activation").css("border", "none");
		launcher_listTxt_activate(txtList_location, txtList_name, txtList_cellno, txtList_carrier, txtList_key);
	}
});
}
var setup_txtActivate = function() {
	$(".listAlertText").css("display", "none");
	$(".listAlertText_val").css("display", "none");
	var txtList_location = $("#signupData").attr("location");
	var txtList_name = $("#signupData").attr("name");
	var txtList_cellno = $("#signupData").attr("cellno");
	var txtList_carrier = $("#signupData").attr("carrier");
	var txtList_status = $("#signupData").attr("status");
	if (txtList_status == "active") {
		$(".listAlertText").css("display", "block");
	} else {
		$(".listAlertText").css("display", "block");
		$("#listTxt_activate").click(function() {
		var txtList_key = $("#txtList_activation").val();
		if (txtList_key == '') {
			$(".listAlertText_val").css("display", "block");
			$("#txtList_activation").css("border", "solid 2px #7e5728");
		} else {
			$("#txtList_activation").css("border", "none");
			$(".listAlertText").css("display", "none");
			$(".listAlertText_val").css("display", "none");
			launcher_listTxt_activate(txtList_location, txtList_name, txtList_cellno, txtList_carrier, txtList_key);
		}
		});
	}
}
var launcher_listTxt_verify = function (txtList_name, txtList_cellno, txtList_carrier, location) {
	$.ajax({
  		url: "function_contactSignup_txt_verify.php?name="+txtList_name+"&cellno="+txtList_cellno+"&carrier="+txtList_carrier+"&location="+location,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			setup_txtVerify();
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("The Headlocks");
				});
				}
	});
}
var launcher_listTxt_activate = function (location, txtList_name, txtList_cellno, txtList_carrier, key) {
	$.ajax({
  		url: "function_contactSignup_txt_activate.php?name="+txtList_name+"&cellno="+txtList_cellno+"&carrier="+txtList_carrier+"&location="+location+"&key="+key,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			setup_txtActivate();
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("The Headlocks");
				});
				}
	});
}

var setup_listEmail = function() {
	var echeckVerify = true;
	$(".listAlertText").css("display", "none");
	$(".listAlertText_email").css("display", "none");
	var emailList_location = $("#sigupSetup").attr("location");
	$("#listEmail_go").click(function() {
	var emailList_name = $("#emailList_name").val();
	var emailList_email = $("#emailList_emailAddr").val();
	
	if (emailList_name == '') {
		$("#emailList_name").css("border", "solid 2px #7e5728");
	} else {
		$("#emailList_name").css("border", "none");
	}
	if (emailList_email == '') {
		$("#emailList_emailAddr").css("border", "solid 2px #7e5728");
	} else {
		$("#emailList_emailAddr").css("border", "none");
	}
	if (echeck(emailList_email) == false) {
		var echeckVerify = false;
		$(".listAlertText_email").css("display", "block");
		$("#emailList_emailAddr").css("border", "solid 2px #7e5728");
	} else {
		$(".listAlertText_email").css("display", "none");
	}
	if (emailList_name == '' || emailList_email == '' || echeckVerify == false) {
		$(".listAlertText").css("display", "block");
		if (echeckVerify == false) {
			$(".listAlertText_email").css("display", "block");
		}
	} else {
		$(".listAlertText").css("display", "none");
		$(".listAlertText_email").css("display", "none");
		launcher_listEmail_join(emailList_name, emailList_email, emailList_location);
	}
});
}
function echeck(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 launcher_listEmail_join = function (emailList_name, emailList_email, location) {
	$.ajax({
  		url: "function_contactSignup_email.php?name="+emailList_name+"&email="+emailList_email+"&location="+location,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("The Headlocks");
				});
				}
	});
}

var initialize = function() {
	//alert('google');
    // Initialize default values
    var googleLocation = "Null";

    // If ClientLocation was filled in by the loader, use that info instead
    if (google.loader.ClientLocation) {
      googleLocation = getFormattedLocation();
	  //alert(googleLocation);
    }
    //document.getElementById("location").innerHTML = location;
	$('#location').attr('data', googleLocation);
	//alert("location is "+$('#location').attr('data'));
  }
var getFormattedLocation = function() {
    if (google.loader.ClientLocation.address.country_code == "US" &&
      google.loader.ClientLocation.address.region) {
      return google.loader.ClientLocation.address.city + ", " 
          + google.loader.ClientLocation.address.region.toUpperCase();
    } else {
      return  google.loader.ClientLocation.address.city + ", "
          + google.loader.ClientLocation.address.country_code;
    }
  }
  
var renderVideo = function(tumblr_id, tumblr_url, tumblr_width, tumblr_height, tumble_flashvars) {
	
	generateEmbedCode = "<embed width='640' height='360' flashvars='file="+tumblr_url+"&amp;"+tumble_flashvars+"' allowfullscreen='true' class='video_player' quality='high' bgcolor='#000000' src='http://assets.tumblr.com/swf/video_player.swf' type='application/x-shockwave-flash'>";
	
	$(".embed_video").empty();
    $(".embed_video").append(generateEmbedCode);

}
