jQuery(window).load(function(){

	var panels = new Array();
	var navlinks = new Array();
	var linksections = new Array();

	
	Cufon.replace("h2, h3");
	Cufon.replace("#Menu a",{hover:true});

	jQuery("#Nav a:not(.smallpanelsNav), .panelPopup").each(function(i){
        
		var $navitem = jQuery(this);
		var link = $navitem.attr('href');
		var cleanLink = link.split("/");		
		var url = location.href;
		var urlParams = url.split("/");
		var dir = urlParams[3];
		var newLink;
		var cleanLinkVal;

		if(dir == "" || cleanLink[1] == "contact"){
			newLink = "../includes/"+cleanLink[1]+".php"; // Path to include
			cleanLinkVal = cleanLink[1];
		}else{
			if(cleanLink[2] == ""){
				//newLink = "../"+dir+"/"+cleanLink[1]+".php"; // Path to include
				newLink = "../includes/"+cleanLink[1]+".php"; // Path to include
				cleanLinkVal = cleanLink[1];
			}else{
				var query = cleanLink[2].split("=");
				newLink = "../"+dir+"/"+query[1]+".php"; // Path to include
				cleanLinkVal = query[1];
			}			
		}
		
		navlinks[i] = newLink;
		linksections[i] = cleanLinkVal;
		$navitem.click(function(event) {

			event.preventDefault();

			jQuery('#Nav li').removeClass('selected');
			$navitem.parent().addClass("selected");

			if(jQuery(smallPanels).length){
				jQuery(smallPanels).each(function(j){
					jQuery(this).animate({top:460+'px'},500,"linear");
				});
			}

			jQuery("#PanelSliderHolder").show();
			jQuery("#PanelSliderLiner").animate({top:0}, 500, "linear", function() {
				jQuery("#PanelSliderHolder").css("background","#393b3b");
			});
			
			if(panels[cleanLinkVal]) {
				for(var x in panels) {
                    if(cleanLinkVal == x) {
                        panels[x].css({'display':'block'}).animate({opacity:1});
                    } else {
                        panels[x].animate({opacity:0}, 500, function() {
                            jQuery(this).css({'display':'none'});
                        });
                    }
				}
			} else {
				jQuery.get(newLink, null, function(data) {

					if(jQuery("#PanelSliderLiner").find(".panel").length) {
						jQuery("#PanelSliderLiner div.panel").animate({opacity:0}, 500, function() {
							jQuery(this).css({'display':'none'});
						});
					}
                    jQuery("#PanelSliderLiner").append(data);
                    panels[cleanLinkVal] = jQuery("#PanelSliderLiner").find('.' + cleanLinkVal);

                    initPanelContent(cleanLinkVal);
                    jQuery("#PanelSliderLiner").removeClass("loading");
				});
			}
		});
	});


	function initPanelContent(panelIndex) {

        if(jQuery('#PanelSliderHolder div.' + panelIndex + ' #Map').length) {
            var address = jQuery("#PanelSliderLiner").find("#Map").find('span').text();
            initialize(address);
        }
        if(jQuery('#PanelSliderHolder div.' + panelIndex + '.gallery').length) {
            jQuery(".gallery a[rel='lightbox']").lightBox({
                fixedNavigation:true,
                imageBtnPrev: '../images/prevlabel.jpg',
                imageBtnNext: '../images/nextlabel.jpg',
                imageBtnClose: '../images/icon-close.png',
                overlayOpacity:0
            });
        }

	}

	var smallNavitems = new Array();
	var smallPanels = new Array();

	jQuery(".smallPanel").each(function(i){
		smallPanels[i] = jQuery(this);
	});


	jQuery("a.vfade01").click(function () {
		jQuery("div.info01").fadeOut(1);
		jQuery("div.info02").fadeIn(1000);
		return false;
	});
	jQuery("a.vfade02").click(function () {
		jQuery("div.info02").fadeOut(1);
		jQuery("div.info01").fadeIn(1000);
		return false;
	}); 


	jQuery("#Nav a.smallpanelsNav").each(function(i){
		smallNavitems[i] = jQuery(this);
		jQuery(this).click(function(event){
			event.preventDefault();
			jQuery(smallPanels).each(function(x){
				if(x==i){
					jQuery(smallNavitems[i]).parent().addClass("selected");
					jQuery(smallPanels[i]).animate({top:460+'px'},500,"linear");
					jQuery(smallPanels[i]).find(".accordionHolder").fadeOut();
				}else{
					if(jQuery(smallNavitems[x]).parent().hasClass("selected")) jQuery(smallNavitems[x]).parent().removeClass("selected");
					jQuery(smallPanels[x]).animate({top:0+'px'},500,"linear");
					jQuery(smallPanels[x]).find(".accordionHolder").fadeIn();
				}
				//if(jQuery(panels).length){
					jQuery("#PanelSliderHolder").css("background","none");

					jQuery("#PanelSliderLiner").animate({top:460+'px'}, 500, "linear",function(){
						jQuery("#PanelSliderHolder").hide();
					});
				//}
			});
		});
	});

	jQuery(".exhibitorsPanel").hover(function(){
		jQuery(this).animate({top:0+'px'},500,"linear");
		jQuery(".exhibitorsPanel .accordionHolder").fadeIn();
	});

	
	jQuery(".closeSlider").livequery(function(){
		jQuery(this).click(function(event){
			event.preventDefault();
			jQuery("#PanelSliderHolder").css('background','none');
			jQuery("#PanelSliderLiner").animate({top:371+'px'},500,"linear",function(){						
				jQuery("#PanelSliderHolder").hide();																  
			});
		});											
	});
	

	jQuery(".closePanel").click(function(event){
		event.preventDefault();
		jQuery(this).parent().parent().animate({top:460+'px'},500,"linear",function(){
//			jQuery(this).parent().find(".slider//").each(function(i){
//				if(i==0){
//					jQuery(this).addClass('open//');
//				}else{
//					jQuery(this).removeClass('open//');
//				}
//			});
		});
	});

	jQuery('input[type="checkbox"]').livequery(function(){
		jQuery(this).StyledCheckboxField({width:11,height:12});
	});

	jQuery('#ContactForm').livequery(function(){
		jQuery(this).ajaxForm({
			beforeSubmit:function(){
				jQuery('.bottomFields').addClass('loading');
				jQuery("#ContactForm .error").removeClass('error');
			},
			dataType:'json',
			success:function(responseText){
				jQuery('.bottomFields').removeClass('loading');
				if(responseText.Errors){
					jQuery('#FormMessage').empty();
					jQuery(responseText.Errors).each(function(i){
						var id = responseText['Errors'][i];
						jQuery("#"+id).parent().parent().addClass('error');
					});
				}else{
					jQuery('#FormMessage').html(responseText['Success']);
					jQuery('#ContactForm').find(".error").each(function(){
						jQuery(this).removeClass('error');
					});
				}
			}
		});
	});

    var slider = function() {
        var $slider = jQuery(this);
        if (!$slider.hasClass('open')) {
            jQuery('div.open', $slider.parent()).animate( {height:'35px'},
                {duration:500, queue:false, easing:"linear",
                    complete:function() {
                        jQuery(this).removeClass('open');
                        Cufon.refresh();
                    }
                }
            );
            $slider.addClass('open').animate( {height:'205px'},
                {duration:500, queue:false, easing:'linear',
                    complete:function() {
                        Cufon.refresh();
                    }
                }
            );
                
        }
	};
	jQuery("div.exhibitorsPanel div.slider").click(slider);
	jQuery("div.visitorsPanel div.slider").click(slider);

	theRotator();
});

function initialize(addressForMap) {
  if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("Map"));
	var geocoder = new GClientGeocoder();

	function showAddress(address) {
	  geocoder.getLatLng(
		address,
		function(point) {
		  if (!point) {
			jQuery('#Map').html(address + " not found");
		  } else {
			map.setCenter(point, 15);
			var marker = new GMarker(point);
			map.addOverlay(marker);
			map.setUIToDefault();
		  }
		}
	  );
	}
	showAddress(addressForMap);
  }
}

function theRotator() {
	//Set the opacity of all images to 0
	$('div#ImageRotator ul li').css({opacity: 0.0});
	//$('div#DotRotator ul li').css({background-position: left -6px});

	//Get the first image and display it (gets set to full opacity)
	$('div#ImageRotator ul li:first').css({opacity: 1.0});
	//$('div#DotRotator ul li:first').css({background-position: left -6px});

	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',4000);
}

function rotate() {
	//Get the first image
	var current = ($('div#ImageRotator ul li.show')?  $('div#ImageRotator ul li.show') : $('div#ImageRotator ul li:first'));
	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#ImageRotator ul li:first') :current.next()) : $('div#ImageRotator ul li:first'));

	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000).removeClass('show');

	var currentDot = ($('#DotRotator li.show')?  $('#DotRotator li.show') : $('#DotRotator li:first'));
	//Get next dot, when it reaches the end, rotate it back to the first dot
	var nextDot = ((currentDot.next().length) ? ((currentDot.next().hasClass('show')) ? $('#DotRotator li:first') :currentDot.next()) : $('#DotRotator li:first'));

	//Add the 'show' class to the next dot
	nextDot.addClass('show');

	//Remove the 'show' class from previous dot
	currentDot.removeClass('show');
}




