txt_search = "Search";
playerEnabled = 0;
stillIn = 1;
var player;

function startFP(play) {
	if (play == 0) {
		
		//$("player").play();
		//$f("player").playlist(".playlist", {loop:true});
	} else {
		
		if (isMobile) {
			//$f("player").playlist(".playlist", {loop:true});
		} else {
			$f("player").playlist(".playlist", {loop:true});
		}
	}
	
}

function buildFP() {
	player = $f("player", { src: "swf/flowplayer.commercial-3.2.7.swf", wmode: "opaque"}, {
		canvas: {
			// configure background properties 
		    background: '#0000ff', 

		    // remove default canvas gradient 
		    backgroundGradient: 'none', 

		    // setup a light-blue border 
		    border:'none' 		
		},
		clip: {
			//autoPlay: true,
			loop: false,
			//autoBuffering: false,
			//forceStop: false,
			scaling: "fit",
			onStart: function() {
				this.getPlugin("play").css({opacity: 0});
				
				obj = $(".playlist .playing").children(".info");
				
				//alert(obj.length);
				
				if (obj.length > 0) 
				{
					name = obj.children("h4").html();
					name2 = obj.children("h5").html();
					
					$(".innerSmallVideo").children("h4").html(name2);
					$(".innerSmallVideo").children("h3").html(name);
				}
				
				

			}		
		},
		
		/*play: {
			url: '/images/largePlay.png',
			width: 132,
			height: 132
		},*/
		
		
		plugins: {
			
			/*
			myContent: {
				url: "flowplayer.content-3.2.0.swf",
				bottom: 0,
				right: 0,
				width: 20,
				html: "TEST",
				onClick: function() {
					$f().toggleFullscreen();
				}
			},
			*/
			controls:{
				height: 37,
				backgroundColor: "rgba(26,27,30,1)",
				volume: false,
				progressColor: "#ffffff",
				bufferColor: "#1a1b1e",
				sliderColor: "#1a1b1e",
				sliderBorder: "none",
				timeColor: "#ffffff",
				durationColor: "rgba(0,0,0,0.0)",
				//buttonColor: "#62513a",
				//buttonOverColor: "#62513a",
				timeBgColor: "rgba(0,0,0,0.0)",
				timeBorderColor: "rgba(0,0,0,0.0)",
				backgroundGradient: "none",
				scrubberBarHeightRatio: "1",
				timeFontSize: "13",
				timeSeparator: " ",
				autoHide: "always",
				tooltipColor: "#000000",
				url: "swf/flowplayer.controls-air-3.2.5.swf"
			}			

			
		},
		
		key: ["$dec8dfac135fcc27f58", "$24844c73d752d9da9f3"]
	//}).controls("hulu", {duration: 25});
	}).ipad();
		
		
}

(function($){

var Core = {
	ondomready: function(){
	// page functions go here
		Core.linkLogo();
		Core.buildSearch();
		Core.buildContact();
		Core.buildHovers();
		Core.buildFPHolder();
		Core.buildFilter();
		Core.buildRequestReel();
		Core.handleReelRequest();
		Core.buildNewsMore();
		Core.anchorFooter();
		
		// disable context menu
		$(document).bind("contextmenu",function(e){
			return false;
		});
		
		
	},
	
	anchorFooter: function(num) {
		var wHeight = $(window).height();
		var dHeight = $("body").height();
		
		if (typeof num == "undefined") {
			if (dHeight < wHeight) {
				$("#footer").toggleClass("fixFooter", true);
			} else {
				$("#footer").toggleClass("fixFooter", false);
			}		
		} else {
		
			if (num == 1) {
				$("#footer").toggleClass("fixFooter", true);
			}
			
			if (num == 0) {
				$("#footer").toggleClass("fixFooter", false);
			}
			
			if (num == 2) {
				vHeight = $("#videoWindow").height();
				//alert(vHeight + " / " + dHeight + " / " + wHeight);

				if ((dHeight - vHeight) > wHeight) {
					$("#footer").toggleClass("fixFooter", false);
				}
			} 
			
		}
	},
	
	
	playVideo: function() {
		Core.revealPlayer();
	},
	
	autoplayReel: function() {
		$(".playlist a:first").click();
	},
	
	buildRequestReel: function() {
//		$("#requestReel").center();
		$(".testRow").hide();
		$("a.requestReel").click(function(e) {
			e.preventDefault();
			$(".requestForm").show();
			$(".requestThank").hide();
			$("#blackbg").fadeIn("fast", function() {
				$("#requestReel").fadeIn("fast");
			});
		});
		
		$("#requestClose a").click(function(e) {
			e.preventDefault();
			$("#requestReel").fadeOut("fast", function() {
				$("#blackbg").fadeOut("fast");
			});			
		});
	},
	
	updateHighlight: function() {
		$(".filternav").each(function() {
			var tarWidth = $(this).children("a.active").width();
			var tarPos = $(this).children("a.active").position();
				
			var lh = $(this).children(".linkHighlight");
			lh.animate({left: tarPos.left+"px", width: tarWidth}, {queue: false, duration: 500, complete: function() {}});
			/*
			lh.animate({left: tarPos.left+"px", width: tarWidth+"px"}, {queue: false, duration: 500, complete: function() {
				//$(".filternav .linkHighlight").width(tarWidth);
				$(".filternav .linkHighlight").animate({width: tarWidth}, {duration: 500});
			}});
			*/
			
		});
	},


	
	buildNewsMore: function() {
		$("#moreNews a").click(function(e) {
			e.preventDefault();
			$("#newsListing").append("<div class='newsHolder loading'></div>");
			var cur = $("#newsListing .story").length;
			
			$.ajax({
				type: "GET",
				url: "js/ajax/news.php?limit="+cur, 
				success: handleNewsLoad
			});
			
		});
	},
	
	buildFilter: function() {
		$(".filternav").append("<div class='linkHighlight'></div>");
		$(".filternav a").click(function(e) {
			e.preventDefault();
			
			var tag = $(this).attr("href");
			$(".tilingcontent").html('<div id="waiting"></div>');
			
			$(".tilingcontent").load("js/ajax/reel.php?tag="+tag, handleFilterLoad);
			$(".filternav a[href='"+tag+"']").toggleClass("active", true);
			$(".filternav a[href!='"+tag+"']").toggleClass("active", false);
			
			Core.anchorFooter();
			//Core.updateHighlight();
			
		});
		
		Core.updateHighlight();
	},
	
	buildFPHolder: function() {
		/*
		$("#player").hover(function() {
			$("#hulu").fadeIn("fast");
		}, function() {
			$("#hulu").fadeOut("fast");
		});
		*/
		
		//$f("player").toggleFullscreen();
	},
	
	revealPlayer: function() {
		$("#videoWindow").animate({height: "520px"}, 500, Core.anchorFooter);
	},
	
	hidePlayer: function() {
		
		$("#videoWindow").animate({height: "0px"}, 500, Core.anchorFooter);
		$f("player").stop();
		//$("#videoWindow").slideUp("slow");
		//$f("player").stop();
	},
	
	handleReelRequest: function() {
		$("#reelForm").submit(function(e) {
			e.preventDefault();
			
			name=$("#name").val();
			email=$("#email").val();
			company=$("#company").val();
			address=$("#address").val();
			city = $("#city").val();
			
			url = "formHandler.php";
			
			if (name != "" || email != "" || company != "" || address != "")
			{
			
				$.post( url, {verified: 2, city: city, name: name, email: email, company: company, address: address},
					function(data) {
						$(".requestForm").hide();
						$(".requestThank").show();
						setTimeout("Core.closeRequest();", 2000);
						
					}
				);
			
			}
			
		});
	},
	
	closeRequest: function() {
		$("#requestClose a").click();
	},
	
	buildHovers: function() {
		$(".sectionClose a").click(function(e) {
			e.preventDefault();
			Core.hidePlayer();
		});
	
		$(".playlist a").click(function(e) {
			$("#footer").toggleClass("fixFooter", false);
			if (playerEnabled == 0) {
				//e.preventDefault();
				playerEnabled = 1;
				Core.revealPlayer();
				startFP(1);
				
			} else {
				Core.revealPlayer();
				startFP(0);
			}
		});
	
		$("#footer a").not("#beyond a").not(".linkStyle1").hover(function() {
			$(this).animate({color: "#8b8b8b"}, {queue: false, duration: 300});
		}, function() {
			$(this).animate({color: "#000000"}, {queue: false, duration: 300});
		});
		
		$("#page_contact .footerRow .footerCell a").not(".linkStyle1").hover(function() {
			$(this).animate({color: "#8b8b8b"}, {queue: false, duration: 300});
		}, function() {
			$(this).animate({color: "#000000"}, {queue: false, duration: 300});
		});
		
		$("#page_contact .contactRow .footerCell a").not(".linkStyle1").hover(function() {
			$(this).animate({color: "#8b8b8b"}, {queue: false, duration: 300});
		}, function() {
			$(this).animate({color: "#ffffff"}, {queue: false, duration: 300});
		});
		
		
		
		$(".tile a, a.tile").hover(function() {
			$(this).children("img").animate({borderTopColor: "#353535", borderLeftColor: "#353535", borderBottomColor: "#353535", borderRightColor: "#353535"}, {queue: false, duration: 500});
		}, function() {
			$(this).children("img").animate({borderTopColor: "#272727", borderLeftColor: "#272727", borderBottomColor: "#272727", borderRightColor: "#272727"}, {queue: false, duration: 500});
		});
		
		$(".tile").click(function(e) {
			//e.preventDefault();
			
			/*
			if ($("body").scrollTop() > 500)
				$("html, body").delay(0).animate({scrollTop: 0}, "fast");
			*/
		});		
		
		
		$(".tags .filtertag").unbind("click");
		$(".tags .filtertag").click(function(e) {
			e.preventDefault();
			$(".filternav a[href='"+$(this).attr("href")+"']").click();
		});
		
		
		/*
		$("#top a, #supertop a").hover(function() {
			$(this).animate({color: "#7d7d7d"}, {queue: false, duration: 300});
		}, function() {
			$(this).animate({color: "#ffffff"}, {queue: false, duration: 300});
		});
		
		

		$("a.linkStyle1, a.more").hover(function() {
			$(this).animate({color: "#c8c8c8"}, {queue: false, duration: 300});
		}, function() {
			$(this).animate({color: "#838383"}, {queue: false, duration: 300});
		});

		$(".linkStyle2").not("span.linkStyle2").hover(function() {
			$(this).animate({color: "#c8c8c8"}, {queue: false, duration: 300});
		}, function() {
			$(this).animate({color: "#838383"}, {queue: false, duration: 300});
		});
		
		$(".sectionnav a").not(".active").hover(function() {
			$(this).animate({color: "#c8c8c8"}, {queue: false, duration: 300});
		}, function() {
			$(this).animate({color: "#838383"}, {queue: false, duration: 300});
		});
		
		$(".section .sectioncontent .story a.more").not("#page_news .story a.more").hover(function() {
			$(this).animate({color: "#7d7d7d"}, {queue: false, duration: 300});
		}, function() {
			$(this).animate({color: "#ffffff"}, {queue: false, duration: 300});
		});
		*/
			
	},
	
	buildContact: function() {
		$("#contact, #aboutContact").click(function(e) {
			e.preventDefault();
			$("html, body").animate({
				scrollTop: $("#footer").offset().top
			}, 1000, "linear");
		});
	},
	
	linkLogo: function() {
		$("#logo").click(function(e) {
			e.preventDefault();
			document.location = "index.php";
		});
	},
	
	handleLeave: function() {
		if (stillIn == 0)
		{
			$("#search").toggleClass("activeSearch", false);
			var val = $("#search .search").val();
			if (val == "") $("#search .search").val(txt_search);
			$("#search").animate({backgroundColor: "#000000"}, {queue: false, duration: 500, complete: function() {
				$("#search").css("background-color", "transparent");
				
			}});
			$("#search select, #search .submitButton").fadeOut("slow"); //({opacity: 1}, {queue: false, duration: 500});

		}
	},
	
	
	buildSearch: function() {
		$("#search .search").click(function() {
			$("#search").css("background-color", "#000000");
			$("#search").animate({backgroundColor: "#242424"}, {queue: false, duration: 500});
			
			$("#search select, #search .submitButton").fadeIn("slow"); //({opacity: 1}, {queue: false, duration: 500});
			
			$("#search").toggleClass("activeSearch", true);
			var val = $("#search .search").val();
			if (val == txt_search) $("#search .search").val("");
			
			
		});
	
		$("#search .search").hover(function() {
			stillIn = 1;
		}, function() {
			stillIn = 0;
		});
		
		var config = {
			out: Core.handleLeave,
			timeout: 300
		};	
	
		$("#search").hoverIntent(config);
	
	/*
		$(".subsearch a").click(function(e) {
			e.preventDefault();
			$(this).parent().toggleClass("subsearchActive", true);
			$(this).parent().children("select").children("option:selected").removeAttr("selected");
			selected = 0;
		});
		
		$(".subsearch select").change(function() {
			var val = $(this).children("option:selected").text();
			$(this).parent().toggleClass("subsearchActive", false)
			var cur = $(this).parent().children("a").children("b").html();
			$(this).parent().children("a").html("<b>"+cur+"</b> "+val);
			selected = 1;
		});
		
		$(".subsearch").hover(function() {
		
		}, function() {
			if (selected == 0) {
				var val = "(select)";
				var cur = $(this).children("a").children("b").html();
				$(this).children("a").html("<b>"+cur+"</b> "+val);		
				$(this).toggleClass("subsearchActive", false);
			}
		});
	*/
	}
	
}

window.Core = Core;

$(document).ready(function(){Core.ondomready()});

})(jQuery);

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

jQuery.fn.hCenter = function () {
    this.css("position","absolute");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


function handleFilterLoad() {
	Core.buildHovers();
	Core.anchorFooter();
	Core.updateHighlight();

	//$f("player").playlist(".playlist", {loop:true});
}	


function handleNewsLoad(data) {

		//$("#newsListing").append(data);
		$(".newsHolder:last").animate({opacity: 0}, 500, function() {
			$(".newsHolder:last").html(data).toggleClass("loading", false).animate({opacity: 1}, 500);
		});
		Core.anchorFooter();
		
};
