// JavaScript Document

var hasFlash = true;
var languageNav = false;
var url_gallery_link = "";
var url_gallry_video = false;
$(document).ready(function(){
	// Common functions
	clearClick();
	setBrowserClass();
	cssTweaks();
	$(document).pngFix();
	
	
	eventsTabs();
	//
	// Show non-flash content
	if(!hasFlash){
		$('.no_flash').show();
		$('.has_flash').remove();
		
	}else{
		$('.no_flash').remove();
		
	}
	
	
	$('#am_gallery').before('<div id="gnav">').cycle({ 
		fx:     	'scrollHorz', 
		speed:  	600, 
		timeout: 	12000,
		prev:    	'#prev', 
		next:    	'#next',
		pager:  	'#gnav'
	});
	
	var language = $.cookie("language");
	
	if(language == null) {
		$.cookie("language", "EN");
	} else {
		$('#language_nav').removeClass().addClass('set'+language);
	}
	$('#set_language a').click(function() {
		
		if(languageNav) {
			languageNav = false;
			language = $(this).text() ;
			$.cookie("language", language)
			
			$("#set_language").animate({ 
				width: "38px"
			  }, 800, function() {
				  $('#language_nav').removeClass().addClass('set'+language);
			  });
			  
			  
			  
		} else {
			languageNav = true;
			$('#language_nav').removeClass().addClass('setEN');
			 $("#set_language").animate({ 
				width: "170px"
			  }, 800 );
		}
	});
	
	// subpage gallery
	if($('#subgallery')) {
		
		$('#subgallery').before('<div id="subgallerynav">').cycle({ 
			fx:     	'fade', 
			speed:  	600, 
			timeout: 	12000,
			pager:  	'#subgallerynav',
			before:	function(currSlideElement, nextSlideElement) {
				//attr('href')
				//alert(forwardFlag);
				url_gallery_link = $(nextSlideElement).children('a').attr('href');
				// check to see if .mov is in the link
				//var pattern = /\.mp4/gi;
				var pattern = /(\.flv|\.mp4)/;
			
				if(url_gallery_link.match(pattern)) {
					url_gallry_video = true;
					$('#btnPlay').fadeIn('slow');
				} else {
					url_gallry_video = false;
					$('#btnPlay').fadeOut('slow');
				}
			}
		});
		
		
		$('#btnPlay').click(function(){
			$('#subgallery').cycle('pause');
			showVideo();
			return false;
		});
		$('#btnClose').click(function(){
			hideVideo();
			return false;
		});
		
		
		$('#subgallery a').click(function(){
			if(url_gallry_video) {
				$('#subgallery').cycle('pause');
				showVideo();
				return false;
			} else {
				return true;
				}
			
		});
	
	}
	
	// home page
	 $(".am_thumb_gallery").jCarouselLite({
        btnNext: "#thumbNext",
		speed: 550,
		visible: 5,
		beforeStart: function(a) {
			Cufon.refresh('#am_thumb_gallery li p');
		},
		afterEnd: function(a) {
			Cufon.refresh('#am_thumb_gallery li p');
		}
    });
	//sfHover();
	if(typeof(Cufon) != "undefined") {
		Cufon.replace('#am_thumb_gallery li p', { fontFamily:'HN' });
		Cufon.replace('#nav > li > a', { fontFamily:'DIN', hover:true });
	}
	//$('marquee').marquee();
	
	
	
	$("ul#nav li:first-child").mouseenter(function() {
		$(this).addClass("shover");
	}).mouseleave(function(){
		$(this).removeClass("shover");
		
	});

	
	
	
	
});


function cssTweaks(){
	$('blockquote').each(function(i,elem){ $(elem).find('p:last').css('margin',0); });
	$('ol li').wrapInner('<span></span>');
}



jQuery.cookie = function (key, value, options) {

    // key and value given, set cookie...
    if (arguments.length > 1 && (value === null || typeof value !== "object")) {
        options = jQuery.extend({}, options);

        if (value === null) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? String(value) : encodeURIComponent(String(value)),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

function eventsTabs() {
	$('.events').hide();
	$('#tab1').click(function() {
		$('.news').show();
		$('.events').hide();
		$('#tab_nav').removeClass().addClass('tab1');
		return false;
	});
	$('#tab2').click(function() {
		$('.news').hide();
		$('.events').show();
		$('#tab_nav').removeClass().addClass('tab2');
		return false;
	});
}


function showVideo() {
	
	$('#btnClose').show();
	//url_gallery_link
	$('#btnPlay').hide();
	var flash = "";
	$('#subgallery_video').html('');
	if (agent.indexOf('ipad') >= 0) {		
		flash += '<video id="vid" src="' + url_gallery_link + '" tabindex="0" autoplay="autoplay" controls="controls"></video>';
		//flash += '<script type="text/javascript">';
		//flash += 'var video = $("#vid").get(0);';
		//flash += 'vid.play();';
		
	} else if(agent.indexOf('firefox') >= 0) {
		flash += '<object width="640" height="380" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab">';
		flash += '<param name="src" value="' + url_gallery_link + '">';
		flash += '<param name="autoplay" value="true">';
		flash += '<param name="controller" value="true">';
		flash += '<embed src="' + url_gallery_link + '" width="640" height="380" autoplay="true" controller="true" pluginspage="http://www.apple.com/quicktime/download/"></embed>';
		flash += '</object>';
	} else {
		flash += '<video id="vid" controls="controls" autoplay="autoplay">';
		flash += '<source src="' + url_gallery_link + '" type="video/mp4" />';
		
		flash += '<object width="640" height="380" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab">';
		flash += '<param name="src" value="' + url_gallery_link + '">';
		flash += '<param name="autoplay" value="true">';
		flash += '<param name="controller" value="true">';
		flash += '<embed src="' + url_gallery_link + '" width="640" height="380" autoplay="true" controller="true" pluginspage="http://www.apple.com/quicktime/download/"></embed>';
		flash += '</object>';
		flash += '</video>';
	}
	
	$('#subgallery_video').html(flash);
	$('#subgallery_video').fadeIn();
}


function hideVideo() {
	$('#subgallery_video').html('');
	$('#subgallery_video').hide();
	$('#btnClose').hide();	
	$('#subgallery').cycle('resume');
	}


function sfHover() {
	
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}







