
var firstRun = true;

var features = ["ciscolive","cee","allen","lockheed"];
var currentFeature = Math.floor(Math.random()*features.length);
var featureTimer;

var isiPad = navigator.userAgent.match(/iPad/i) != null;
var isiPhone = navigator.userAgent.match(/iPhone/i) != null;

var defaultWork = "onetouchhelp";
var defaultNews = "kenny";



$(document).ready(function() {
	
	updateFeeds();
	$('.scroll-pane').jScrollPane();
		
	nextFeature();
	resetForm();
	
	
	$(".navbtn").click(function(event) {

		//event.preventDefault();

		if ($(this).attr('hash') == window.location.hash)	{
			var section = $(this).attr('hash').replace("#","").split("/")[0];
			var subsection = $(this).attr('hash').replace("#","").split("/")[1];
			loadSection(section, subsection);
		}
	
	});


	
});




$.address.change(function(event) {

	var url = event.value;

	var section = url.split("/")[0];
	var subsection = url.split("/")[1];
	var subsubsection = url.split("/")[2];

	if (section == "")	{
		loadSection("feature", "");
	} else	{
		loadSection(section, subsection);
	}
	

	if (firstRun)	{
		if (((section == "work") && (subsection == undefined)) || (section != "work"))	{
			loadWork(defaultWork);
		}
		if (((section == "news") && (subsection == undefined)) || (section != "news"))	{
			loadNews(defaultNews);
		}
	}
	
	firstRun = false;	
	
});



function nextFeature()
{
	loadFeature(features[currentFeature]);
	currentFeature++;
	if (currentFeature >= features.length)	{
		currentFeature = 0;
	}
	featureTimer = setTimeout("nextFeature()",30000);
}

function loadSection(section, subsection)
{

/*
	var title = "AI : " + section.toUpperCase();
	if (subsection != undefined)	{
		title += " : " + pTitles[subsection];	
	}	
	$(document).attr("title", title);
*/
	

	$("#sections div.section").each(function() {
		
	   	var id = $(this).attr('id').split("-")[1];
	   	
		$(this).animateToClass("section-" + id + "-close", 500, function() {

			$(this).removeClass("section-" + id + "-open");
			$(this).addClass("section-" + id + "-close");
			
			if (id == section)	{
			
				if ((section == "news") && (subsection != undefined))	{
					loadNews(subsection);
				}
				
				if ((section == "work") && (subsection != undefined))	{
					loadWork(subsection);
				} else	{
					loadWork(defaultWork);
				}
			
			/*
				$.scrollTo($(this), 500, function() {
				
					$("#section-" + section).animateToClass("section-" + section + "-open", 500, function() {
						$("#section-" + section).removeClass("section-" + section + "-close");
						$("#section-" + section).addClass("section-" + section + "-open");
						
						var hash = "";
						if (subsection)	{
							hash = section + "/" + subsection;
						} else	{
							hash = section;						
						}

						setTimeout(function() {window.location.hash = hash;}, 200);

    				});
    				
	    		});
	    	*/
	    	
	    		var top = $(this).offset().top;
	    		
           		$('html,body').animate({scrollTop: top},  function() {
				
					$("#section-" + section).animateToClass("section-" + section + "-open", 500, function() {
						$("#section-" + section).removeClass("section-" + section + "-close");
						$("#section-" + section).addClass("section-" + section + "-open");
						
						/*
						var hash = "";
						if (subsection)	{
							hash = section + "/" + subsection;
						} else	{
							hash = section;						
						}

						window.location.hash = hash;
						*/

    				});
    				
	    		});


	    			
			}
    	});
    	
	});
	
	if (section == "feature")	{

		var top = $("#section-feature").offset().top;
        $('html,body').animate({scrollTop: top},  function() {
			//window.location.hash = section;
	    });

	}
	
	if (section == "contact")	{
		resetForm();
	}


}


function updateFeeds()
{

	$("#twitter0").tweet({
        join_text: "auto",
		auto_join_text_default: "AgencyFish",
        username: "AgencyFish",
        avatar_size: 20,
        count: 1,
        loading_text: "loading tweet..."
      });
      
	$("#twitter1").tweet({
        join_text: "auto",
		auto_join_text_default: "imediatweet",
        username: "imediatweet",
        avatar_size: 20,
        count: 1,
        loading_text: "loading tweet..."
      });

	$("#twitter2").tweet({
        join_text: "auto",
		auto_join_text_default: "bogusky",
        username: "bogusky",
        avatar_size: 24,
        count: 1,
        loading_text: "loading tweet..."
      });

	$("#twitter3").tweet({
        join_text: "auto",
		auto_join_text_default: "DigSignageToday",
        username: "DigSignageToday",
        avatar_size: 24,
        count: 1,
        loading_text: "loading tweet..."
      });
      
	$("#twitter4").tweet({
        join_text: "auto",
		auto_join_text_default: "EdelmanDigital",
        username: "EdelmanDigital",
        avatar_size: 24,
        count: 1,
        loading_text: "loading tweet..."
      });


	$("#twitter5").tweet({
        join_text: "auto",
		auto_join_text_default: "adweek",
        username: "adweek",
        avatar_size: 24,
        count: 1,
        loading_text: "loading tweet..."
      });
 
 	$("#twitter6").tweet({
        join_text: "auto",
		auto_join_text_default: "adage",
        username: "adage",
        avatar_size: 24,
        count: 1,
        loading_text: "loading tweet..."
      });

 	$("#twitter7").tweet({
        join_text: "auto",
		auto_join_text_default: "AdMonsters",
        username: "AdMonsters",
        avatar_size: 24,
        count: 1,
        loading_text: "loading tweet..."
      });

}


function loadFeature( id )	{
	$('#section-feature').fadeTo(0,0);
	
	$.ajax({
		url: "webservice/feature/" + id + ".html",
		dataType: "html",
		success: function(result){
			$('#section-feature').html(result);
			$('#section-feature').fadeTo(1000,1);

			$("#clickhere").click(function() {
		
				if ($(this).attr('hash') == window.location.hash)	{
					var section = $(this).attr('hash').replace("#","").split("/")[0];
					var subsection = $(this).attr('hash').replace("#","").split("/")[1];
					loadSection(section, subsection);
				}
		
			});

		}
	});
	
	
}

function loadNews( id )	{

	$('#news-article').fadeTo(0,0);
	$.ajax({
		url: "webservice/news/" + id + ".html",
		dataType: "html",
		success: function(result){
			$('#news-article').html(result);
			$('#news-article').fadeTo(1000,1);
		}
	});
}

function loadWork( id )	{

	$("#work-nav-items li").each(function() {
		var itemID = $(this).attr('id').split("-")[2];
		
		if (id == itemID)	{
			$(this).addClass("selected");
		} else	{
			$(this).removeClass("selected");
		}
	});

	$('#work-gallery').fadeTo(0,0);

	$.ajax({
		url: "webservice/work/" + id + ".html",
		dataType: "html",
		success: function(result){
			$('#work-gallery').html(result);
			$('#work-gallery').fadeTo(1000,1);
		}
	});
}

function resetForm()	
{
	$.ajax({
		url: "webservice/form.html",
		dataType: "html",
		success: function(result){
			$('#form').html(result);
		}
	});
}

function submitform()
{
	//alert($('#form-name').val());

		$.ajax({
			url: "webservice/form_submit.php",
			type: "POST",
			data: ({name : $('#form-name').val(), email : $('#form-email').val(), phone : $('#form-phone').val(), msg : $('#form-msg').val()}),
			dataType: "html",
			success: function(result){
				$('#form').html(result);
			}
		});
		
}

function showImage(target, image)
{
	$("#" + target).html('<img src="' + image + '" class="image">');
}

function showVideo(target, video)
{

	var htmlText = "";

	if (swfobject.hasFlashPlayerVersion("9"))	{
	
		video = "../" + video;

		htmlText = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="640" height="385" id="videoplayer" name="videoplayer">';
		htmlText += '<param name="movie" value="jwplayer/player.swf">';
		htmlText += '<param name="allowfullscreen" value="true">';
		htmlText += '<param name="allowscriptaccess" value="always">';
		htmlText += '<param name="bgcolor" value="#000000">';
		htmlText += '<param name="flashvars" value="file=' + video + '&autostart=true&menu=false">';
		htmlText += '<embed id="videoplayer" name="videoplayer" src="jwplayer/player.swf" width="640" height="385" allowscriptaccess="always" allowfullscreen="true" bgcolor="#000000" flashvars="file=' + video + '&autostart=true&menu=false">';
		htmlText += '</object>';
		
	} else	{
		
		htmlText = '<video src="' + video + '" width="640" height="360" controls autoplay></video>';
		
	}
	
	$("#" + target).html(htmlText);
	
}


