$(document).ready(function(){


if ((navigator.userAgent.indexOf('iPad') != -1)) {
	$.fn.liScroll = function(settings) {
		settings = $.extend({
		travelocity: 0.09
		}, settings);		
		return this.each(function(){
			var $strip = $(this);
			$strip.addClass("newsticker");
			var stripWidth = 0;
			$strip.find("li.ticker").each(function(i){
			stripWidth += $(this, i).outerWidth(true); // thanks to Michael Haszprunar and Fabien Volpi
			});
			var $mask = $strip.wrap("<div class='mask'></div>");
			var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
			var containerWidthx = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
			$strip.width(stripWidth);			
			var totalTravelx = stripWidth+containerWidthx ;
			var defTimingx = totalTravelx/settings.travelocity;	// thanks to Scott Waye		
			function scrollnews(spazio, tempo){
				$strip.animate({left: '0px'}, tempo, "linear", function(){
					//$strip.css("left", containerWidthx); 
					scrollnews(totalTravelx, defTimingx);
				});
			}
			scrollnews(totalTravelx, defTimingx);
		});	
	};
	$("ul#twitterTicker").liScroll();	

}


    $('ul#twitterTicker').cycle({
       	fx:        'scrollUp',
       	timeout:       10000,
		before: function(curr, next, opts) {
			opts.animOut.opacity = 0;
		}
	});	
		
				   

	// PAGE VARIABLES					   
	$.cookie("tab", "home");
	var count = 0;
	var slide = 1;
	var last = $("div.slideshow div:first").attr("id");
	var timer = 250;
	var backgroundAnimationOn = true;


	// DESKTOP - IPAD BACKGROUND CONTROLLLER
	if ((navigator.userAgent.indexOf('iPad') != -1)) {
		// DO NOTHING
	} else {
		backgroundFade();
	}

	

	var bHeight = $(window).height()-57;	
	var nHeight = $("div#nav").height()+55;
	var nav = $("div#nav");
	var navPosition = nav.position();
	var navTop = navPosition.top;


	// RANDOM BACKGROUND FOR IPAD
	function randomBackground() {
		var backgrounds = new Array();
		$('div.slideshow div.even').each(function(index) {
			var id = $(this).attr("id");
			backgrounds.push(id);
		});
		var selected = backgrounds[Math.floor(Math.random() * backgrounds.length)];
		$("div.slideshow div#"+selected).css("display", "block");
	}
		
	// BACKGROUND ANIMATION FOR DESKTOP
   	function backgroundFade(timer) {
		if (slide%2 == 0) {
			$("div.slideshow div#"+slide).delay(5000).fadeOut(250, function() {
				if (slide == last) { 
					slide = 1;
				} else {
					slide++;	
				}
				backgroundFade();
			});
		} else {
			$("div.slideshow div#"+slide).fadeIn(250, function() {
				$(this).delay(2500).fadeOut(2500);
				slide++;
				$("div.slideshow div#"+slide).fadeIn(2500);
				backgroundFade();
			});
		}
   	};
	
	


	function minHeight() {		
		var bHeight = $(window).height()-57;	
		var nHeight = $("div#nav").height()+55;
		var minWidth = nHeight + 57;
		var nav = $("div#nav");
		var navPosition = nav.position();
		var navTop = navPosition.top;
		if (navTop <= 112) {
			$("div#nav").removeClass("bottom").addClass("top");
			$("div#outer").css("min-height", minWidth+"px");
			$("div.slideshow").css("min-height", minWidth+"px");
		} 
		var bHeight = $(window).height()-57;	
		var nHeight = $("div#nav").height()+55;
		if (bHeight > nHeight) {
			var bHeight = $(window).height()-nHeight + 55;			
			var type = $("div#nav").attr("class");
			if (navTop == 112) {
				$('div#nav').delay(200).animate({top: [bHeight+'px', 'easeOutExpo'], avoidTransforms:true}, 1000, function() {
					$("div#nav").removeAttr("style").removeClass("top").addClass("bottom");	
					$("div#outer").removeAttr("style");
					$("div.slideshow").removeAttr("style");					
				});					
			}
		}						
	};

	// FIRST CLICK
	function firstClick(page) {

		$("div#nav div#page").css("borderColor", "#e52525");		
		$('div#nav div#page').slideDown("normal", "easeOutExpo", function () {

			if (page == "portfolio" || page =="videos") {
				$('div#nav div#page').css("background-image", "none");
				$('div.nav').css("background-image", "url(images/white_bg2.png)");
			}	
			minHeight();
			$('div#nav div#page div.content').css("visibility", "visible").css("opacity", "0.0").fadeTo(200, 1.0, function() {
				if ($.browser.msie)  
				this.style.removeAttribute('filter');									
			});
		});
		var newHeight = $("div#height").height();
		$('div#nav div#page div.content').css("height", newHeight+"px");														
		if (page == "videos") {
			$("ul.vids li").click(function() {

					var videoId = $(this).attr("id");									
					$.ajax({
					url: "index.php/functions/videos/?video="+videoId,
						success: function(data) {
							$('div#nav div#page div.content').animate({height: ['582px', 'easeOutExpo']}, 500, function() {																																		
								//minHeight();																																			
								$("div#videoContent").css("height", "348px").slideDown(500, function() {																					  
									$("div.videoContainer").html(data).slideDown("normal", "easeOutExpo", function() {																																   
										if ($.browser.msie)  
										this.style.removeAttribute('filter');																																   
									});											  
								});																																  
							});					
						}			
					});									   
																						
			});								
		}		
	}
	
	// IPAD SECOND CLICK
	function iPad(page, nooHeight) {
		$("div#nav div#page").css("borderColor", "#fff");
		$('div#nav div#page').slideUp(500, 'easeOutExpo', function() {
			$("div#outer").removeAttr("style");
			var bHeight = $(window).height()-60;				
			var type = $("div#nav").attr("class");
			if (type == "top") {				
				$('div#nav').animate({top: [bHeight+'px', 'easeOutExpo']}, 500, function() {
					$("div#nav").removeAttr("style").removeClass("top").addClass("bottom");	
					$("div#outer").removeAttr("style");
					$("div.slideshow").removeAttr("style");
					$("div#nav div#page").css("borderColor", "#e52525");
					$('div#nav div#page').slideDown(1000, "easeOutExpo", function () {
						if (page == "portfolio" || page =="videos") {
							$('div#nav div#page').css("background-image", "none");
							$('div.nav').css("background-image", "url(images/white_bg2.png)");
						}						
						$('div#nav div#page div.content').css("visibility", "visible").css("opacity", "1.0").fadeTo(200, 1.0, function() {
							if ($.browser.msie)  
							this.style.removeAttribute('filter');									
						});
						minHeight();
					});
				});


			} else {
				$("div#nav div#page").css("borderColor", "#e52525");
				$('div#nav div#page').slideDown(1000, "easeOutExpo", function () {
					if (page == "portfolio" || page =="videos") {
						$('div#nav div#page').css("background-image", "none");
						$('div.nav').css("background-image", "url(images/white_bg2.png)");
					}	
					minHeight();
					$('div#nav div#page div.content').css("visibility", "visible").css("opacity", "1.0").fadeTo(200, 1.0, function() {
						if ($.browser.msie)  
						this.style.removeAttribute('filter');									
					});
				});
			}
		});

		$("div#nav div#page div.content").css("height", "auto");
		$('div#nav div#page').css("background-image", "url(images/white_bg2.png)");
		var newHeight = $("div#height").height();
		$('div#nav div#page div.content').css("height", newHeight+"px");														
		if (page == "videos") {
			$("ul.vids li").click(function() {	



														   
				var videoId = $(this).attr("id");									
				$.ajax({
				url: "index.php/functions/videos/?video="+videoId,
					success: function(data) {
						$('div#nav div#page div.content').animate({height: ['582px', 'easeOutExpo']}, 500, function() {																																		
							minHeight();																																				
							$("div#videoContent").css("height", "348px").slideDown("normal", "easeOutExpo", function() {																					  
								$("div.videoContainer").html(data).slideDown("normal", "easeOutExpo", function() {																																   
									if ($.browser.msie)  
									this.style.removeAttribute('filter');																																   
								});											  
							});																																  
						});					
					}			
				});																							
			});								
		}
	};	
	// DESKTOP SECOND CLICK
	function deskTop(page, newHeight) {
		$('div#nav div#page div.content').animate({height: [newHeight+'px', 'easeOutExpo']}, 500, function() {
			if (page == "portfolio" || page == "videos") {
				$('div#nav div#page').css("background-image", "none");
				$('div.nav').css("background-image", "url(images/white_bg2.png)");
			}																
			$('div#nav div#page div.content').css("visibility", "visible").css("opacity", "0.0").fadeTo(200, 1.0, function() {	
				if ($.browser.msie)  
					this.style.removeAttribute('filter'); 
			});
			minHeight();
		});
		if (page == "videos") {
			$("ul.vids li").click(function() {

				var videoId = $(this).attr("id");									
				$.ajax({
				url: "index.php/functions/videos/?video="+videoId,
					success: function(data) {
						$('div#nav div#page div.content').animate({height: ['582px', 'easeOutExpo']}, 500, function() {
							minHeight();																																																																		
							$("div#videoContent").css("height", "348px").fadeIn(500, function() {																					  
								$("div.videoContainer").html(data).slideDown("normal", "easeOutExpo");
								if ($.browser.msie)  
									this.style.removeAttribute('filter');													
							});																																	  
						});					
					}			
				});						
			});								
		}		
	}
		



	// WHAT HAPPENS WHEN LOGO IS CLICKED
	$("div#nav h1 a").click(function() {
		
		if ((navigator.userAgent.indexOf('iPad') != -1)) {
			$("div#nav div.menu ul li a").removeClass('active');
			if ($("div#nav div#page div.content").is(":hidden")) {
				return false;
			} else {

				$("div#nav div#page div.content").fadeTo(150, 0.01, function(){
					$("div#nav div#page").css("borderColor", "#ffffff");
					$('div#nav div#page').slideUp('normal', 'easeOutExpo', function() {
						$("div#nav div#page div.content").html("");
						$("div#outer").removeAttr("style");
						var bHeight = $(window).height()-60;				
						var type = $("div#nav").attr("class");
						if (type == "top") {
							
							$('div#nav').delay(200).animate({top: [bHeight+'px', 'easeOutExpo']}, 500, function() {
								$("div#nav").removeAttr("style").removeClass("top").addClass("bottom");	
								$("div#outer").removeAttr("style");
								$("div.slideshow").removeAttr("style");					
							});
						}
						
					});
				});
				$("div#nav div#page div.content").css("height", "auto");			
				$.cookie("tab", "home");
				count = 0;
				var newHeight = "0";
				$('div#nav div#page').css("background-image", "url(images/white_bg2.png)");	
			}
			




		} else {
			$("div#nav div.menu ul li a").removeClass('active');
			if ($("div#nav div#page div.content").is(":hidden")) {
				return false;
			} else {					
				
				$("div#nav div#page").css("borderColor", "#ffffff");
				//$("div#nav div#page div.content").fadeTo(150, 0.01);	
				$("div#nav div#page div.content").fadeOut(150);

				$('div#nav div#page').slideUp('slow', 'easeInOutQuint', function() {
					$("div#nav div#page div.content").html("");
					$("div#outer").removeAttr("style");
	
					var bHeight = $(window).height()-60;				
					var type = $("div#nav").attr("class");
					if (type == "top") {
						
						$('div#nav').delay(200).animate({top: [bHeight+'px', 'easeOutExpo']}, 500, function() {
							$("div#nav").removeAttr("style").removeClass("top").addClass("bottom");	
							$("div#outer").removeAttr("style");
							$("div.slideshow").removeAttr("style");					
						});
					}
				});			
						
				$("div#nav div#page div.content").css("height", "auto");			
				$.cookie("tab", "home");
				count = 0;
				var newHeight = "0";
				$('div#nav div#page').css("background-image", "url(images/white_bg2.png)");								
			}
		}
	});

	// MENU FUNCTIONS
	$("div#nav div.menu ul li a").click(function() {
		if ($(this).attr('id') == 'videos' || $(this).attr('id') == 'portfolio') {
			$("div.slideshow div#" + slide).stop(true, true);
			backgroundAnimationOn = false;
		} else {
			if (!backgroundAnimationOn) {
				backgroundFade();
			}
		}
		var curHeight = $("div.content").height();					 
		if ($.cookie('tab') == "videos") {
			$('div#nav div#page').css("background-image", "url(images/white_bg2.png)");
		}
		if ((navigator.userAgent.indexOf('iPad') != -1)) {
			//$("div#nav div#page").css("borderColor", "#ffffff");
		} else {
			$("div#nav div#page").css("borderColor", "#e52525");
		}		
		$("div#nav div.menu ul li a").removeClass('active');												 
		$(this).addClass('active');									 
		if ($.cookie('tab') != null) {
			if ($(this).attr("id") == $.cookie('tab')) {
				return false;	
			} else {
				$("div#nav div#page div.content").fadeTo(150, 0.01);			
				var tab = $(this).attr("id");
				$.cookie("tab", tab);				
				$('div#nav div#page div.content').css("visibility", "hidden");
				var page = $(this).attr("id");

				/*if (page == "portfolio") {
					if ((navigator.userAgent.indexOf('iPad') != -1)) {
						// DO NOTHING
						
					} else {
						$("div#nav").animate({top: ['112px', 'easeOutExpo']}, 500);
					}										
				}*/


				if (page != "portfolio") {
					$('div#nav div#page').css("background-image", "url(images/white_bg2.png)");

				} 
				$.ajax({
					url: "index.php/functions/getpage/?page="+page,
					//cache: false,
						
					success: function(data) {
						$("div#nav div#page div.content").html(data);	
						var newHeight = $("div#height").height();
						if (curHeight > newHeight) {					
							var nooHeight = newHeight - curHeight;
							var nooHeight = nooHeight - nooHeight - nooHeight;
							var nooHeight = "-="+nooHeight;								
						} else {
							var nooHeight = curHeight - newHeight;
							var nooHeight = nooHeight - nooHeight - nooHeight;
							var nooHeight = "+="+nooHeight;	
						}												
						// SECOND + CLICK AFTER PAGE LOAD
						if (count != "0") {	
							if ((navigator.userAgent.indexOf('iPad') != -1)) {
								iPad(page, nooHeight);
							} else {
								deskTop(page, newHeight);
							}
						// FIRST CLICK ON PAGE LOAD
						} else {							
							firstClick(page);							
						}						
						count++;
						var width = $("div#nav").width() - 124;	
						var newWidth = width/216;
						var newWidth2 = Math.round(newWidth) * 216 - 71;		
						var ul = $("ul:last");
						var ulposition = ul.position();
						var right = ulposition.left + 145;
						limit = right - newWidth2;
						limit = limit - limit - limit;						
						if (right <= width) {							
							$("div.nav a#next").fadeOut();		
						}																										
						$('div#portWrapper').css("width", newWidth2+"px");
						$('div#portWrapper').css("max-width", right+"px");
						$('div#portSlides').css("left", "0px");
						var vidWidth = $("div#nav").width() - 164;
						var vidWidth = vidWidth/155;
						var vidWidth = Math.round(vidWidth) * 155;											
						$('div#videoWrapper').css("width", vidWidth+"px");						
						$('div#videosWrapper ul').css("width", vidWidth+"px");	
					}
				});				
			}
		}
	});
});

$(window).resize(function() {
	var bHeight = $(window).height()-57;	
	var nHeight = $("div#nav").height()+55;
	var minWidth = nHeight + 57;
	var nav = $("div#nav");
	navPosition = nav.position();
	navTop = navPosition.top;	
	if (navTop <= 112) {
		$("div#nav").removeClass("bottom").addClass("top");
		$("div#outer").css("min-height", minWidth+"px");
		$("div.slideshow").css("min-height", minWidth+"px");			
	} 	
	if (bHeight > nHeight) {
		$("div#nav").removeClass("top").addClass("bottom");
		$("div#outer").removeAttr("style");
		$("div.slideshow").removeAttr("style");				
	}		
	if ($.cookie('tab') != "videos") {	
		var newHeight = $("div#height").height();	
		$('div#nav div#page div.content').css("height", newHeight+"px");	
	}
});










