/**
* Requires: jQuery
*/
function OurWorks(show, industry, page){
	if(!is_int(page)) page = 1;
	$("#ea_projects").fadeOut("slow", function () {
		$.ajax({
		   type: 'GET',
		   url: 'index.php',
		   dataType: "json",
		   data: 'class=Product&method=FilterProjects&p[]='+show+'&p[]='+industry+'&p[]='+page,
		   success: function(response){
			pageCount = response.pageCount;
			if(pageCount > 1){
				$('#mcnav').attr('style', 'display:block;');
				if(page < pageCount) $('#mcnav li.next').attr('style', 'display:block;');
				else $('#mcnav li.next').attr('style', 'display:none;');
				if(page > 1) $('#mcnav li.prev').attr('style', 'display:block;');
				else $('#mcnav li.prev').attr('style', 'display:none;');
			}
			else $('#mcnav').attr('style', 'display:none;');
			
			// Loads html content
			$('#ea_projects').html(response.html);
			
			//additional JS elements
			
			$('.gal_link a').colorbox({
			    onComplete: function(){
				if($('#flowPlayer').length > 0){
				    flowplayer('flowPlayer', '/includes/flowplayer-3.2.4.swf');
				}
			    },
			    maxWidth: '1024px',
			    maxHeight: '768px',
			    scrolling: false
			});
			
			$('#ea_projects a[href^=http]').each(function() {
				if(this.href.indexOf(location.hostname) == -1) {
				$(this).click(function(){window.open(this.href);return false;});
				}
			});
			
			$("#ea_projects").fadeIn("slow");
		   }
		 });
	});
}

function is_int(input){
	return typeof(input)=='number' && parseInt(input)==input;
}

function redirect(hashString){
	window.location.hash = hashString;
}

//////////////
// Deep Linking System
//////////////
function handleChange(evt){
    var url1 = evt.pathNames[1] == undefined ? 'all' : evt.pathNames[1];
    var url2 = evt.pathNames[2] == undefined ? 'all' : evt.pathNames[2];
    var url3 = evt.pathNames[3] == undefined ? 1 : evt.pathNames[3];
    $("#show option[value='"+url1+"']").attr('selected', 'selected');
    $("#industry option[value='"+url2+"']").attr('selected', 'selected');
    $("#show, #industry").resetSS();
    $('#show, #industry').change(function() {
		redirect('!/' + $('#show').val()+'/'+$('#industry').val());
    });
    $('#mcnav .prev').click(function() {
		$('html, body').animate({scrollTop: $("#content").offset().top}, 500);
		var prevPage = parseInt(url3) - 1;
		redirect('!/'+url1+'/'+url2+'/'+prevPage);
    });
    $('#mcnav .next').click(function() {
		$('html, body').animate({scrollTop: $("#content").offset().top}, 500);
		var nextPage = parseInt(url3) + 1;
		redirect('!/'+url1+'/'+url2+'/'+nextPage);
    });
    OurWorks(url1, url2, parseInt(url3));
}
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
