// global JS object
chimairaGlobalJS = function() {


		//global vars
		var firstTab;

	return{
		
		/************************
			Init function
			-- attach event handlers
			-- fire drop down menu if present
			-- country select disable for forms
		
		************************/
		init: function(){
			
			
			// drop down menus if LI has UL menu will fire
			$("#navigation-items, #photos-gal-navv").children("li").hover(
				function(){
					$(this).children("ul").show();
					if($(this).children("ul").children("li").children("ul")){
						$(this).children("ul").children("li").hover(
							function(){
								$(this).children("ul").show();
							},
							function(){
								$(this).children("ul").hide();
							}
						);
					}
				},
				function(){
					$(this).children("ul").hide();
				});
				// end drop down
				
			
			//releases
			
			
			//console.log(frontPageGal)
			//build flickr front page
			//console.log(frontPageGal)
			if(frontPageGal != undefined){
				
				if (frontPageGal != '') {
					fontPageGal = frontPageGal
				}else {
					frontPageGal = "72157623254504423";
				}
			}else {
				
				frontPageGal = "72157623254504423";
			}
			
			
			
			$.getJSON("http://api.flickr.com/services/feeds/photoset.gne?count=6&set="+frontPageGal+"&nsid=47357250@N04&lang=en-us&format=json&jsoncallback=?", function(data){
			  $.each(data.items, function(i,item){
				
				if(i == 6){
				
					return false
					
				} else {
				var sourceSquare = (item.media.m).replace("_m.jpg", "_s.jpg");
				
				var sourceLarge = (item.media.m).replace("_m.jpg", ".jpg");
				
				$("#flickr-cont").append("<div id='flickr-"+i+"'><a href='"+ sourceLarge +"' title='"+ item.title+ "'><img src='"+sourceSquare+"'></a></div>")
				}
				
				
			  });
			  
			 //instanciate lightbox
			$("#flickr-cont a, a.lb").lightBox(
								{
									imageLoading: 'http://selkirkarmshotel.co.uk/images/lightbox-ico-loading.gif',
									imageBtnClose: 'http://selkirkarmshotel.co.uk/images/lightbox-btn-close.gif',
									imageBtnPrev: 'http://selkirkarmshotel.co.uk/images/lightbox-btn-prev.gif',
									imageBtnNext: 'http://selkirkarmshotel.co.uk/images/lightbox-btn-next.gif'
								}
							);
			  
			});
			
			
			
			//run gallery
			if($("#selkirk-photo-gallery").length > 0){
				
				//console.log($("#chimaira-photo-gallery"))
				
				this.photoGalleryInit();
				
			}

			
			//switch gallery
			$("#photos-gal-nav a").click(this.photoGallerySwitch);
			
			
			//run releases
			if($("#releases").length > 0){
				
				firstTab ="releases_albums";
				
				this.releaseTabs();
				
			}
			
			if($("#band").length > 0){
				
				firstTab ="theband_bio";
				
				this.releaseTabs();
				
			}
			
			//releases
			$("#rel-nav a").click(this.releaseTabs);	
	
	
			//run map
			if($("#map-cont").length > 0){
			
				this.tourMap();
				
			}
			
			//$("#tour-table").tablesorter( {sortList: [[1,0]]} );
			
			$("#tour-table tbody.vevent").hover(
			
				function(){
					$(this).addClass("on")
				},
				function(){
					$(this).removeClass("on")
				}
			)
			
			//$("#tour-table tbody tr").click(this.tourTable);
			
			
			$("a.tour-toggle").click(this.tourTableToggle);
			
			
			
	
		},
		
		photoGalleryInit : function(e){
			
			
				$("#photo-loader").show();
				
				var firstSet = $("#photos-gal-nav ul :first-child a").attr("rel");
				
				
				
				//console.log(firstSet)
				
				$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=12f8b68640aea2f5bf97bfcd442fe68c&photoset_id="+firstSet+"&extras=url_sq%2Curl_m%2Ctags&format=json&jsoncallback=?", function(data){
					
					$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getInfo&api_key=12f8b68640aea2f5bf97bfcd442fe68c&photoset_id="+firstSet+"&format=json&jsoncallback=?", 
							  function(rData){
								  	
									//console.log(rData);						
										//var trimmedTitle = (data.title).replace("Content from", "")
					
										$("#photo-gallery-title strong").text(rData.photoset.title._content)
										$("#photo-gallery-title span").text(rData.photoset.description._content)
									
							  })
					
					$("#photo-loader").hide();
					
					//console.log(data)
					
					
					
					
					
					$.each(data.photoset.photo, function(i,item){
						
							
							//var sourceSquare = (item.media.m).replace("_m.jpg", "_s.jpg");
							
							//var sourceLarge = (item.media.m).replace("_m.jpg", ".jpg");
							
							$("#photo-gallery-cont").append("<li><a href='"+ item.url_m +"' title='"+ item.title+ "'><img src='"+ item.url_sq +"'></a></li>")
							
						
						
					  });
				  
				 	//instanciate lightbox
					$("#photo-gallery-cont a").lightBox(
								{
									imageLoading: 'http://selkirkarmshotel.co.uk/images/lightbox-ico-loading.gif',
									imageBtnClose: 'http://selkirkarmshotel.co.uk/images/lightbox-btn-close.gif',
									imageBtnPrev: 'http://selkirkarmshotel.co.uk/images/lightbox-btn-prev.gif',
									imageBtnNext: 'http://selkirkarmshotel.co.uk/images/lightbox-btn-next.gif'
								}
							);
				  
				
				});
				
		
		},
		
		photoGallerySwitch : function(e){
			
				e.preventDefault();
				
				
				if ($(this).attr("rel") != ''){
				
				$("#photo-gallery-wrap").hide()
				$("#photo-gallery-cont").html('')
				$("#photo-loader").show();
				
				
				//console.log($(this).attr("rel"))
				

				var newSet = $(this).attr("rel")
			
				$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=12f8b68640aea2f5bf97bfcd442fe68c&photoset_id="+newSet+"&extras=url_sq%2Curl_m%2Ctags&format=json&jsoncallback=?", function(data){
					
					
					$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getInfo&api_key=12f8b68640aea2f5bf97bfcd442fe68c&photoset_id="+newSet+"&format=json&jsoncallback=?", 
							  function(rData){
								  	
									//console.log(rData);						
										//var trimmedTitle = (data.title).replace("Content from", "")
					
										$("#photo-gallery-title strong").text(rData.photoset.title._content)
										$("#photo-gallery-title span").text(rData.photoset.description._content)
										
										$("#photo-loader").hide();
										$("#photo-gallery-wrap").show()
									
							  })
					
					
					
					//var trimmedTitle = (data.title).replace("Content from", "")
					
					//$("#photo-gallery-title strong").text(trimmedTitle)
					//$("#photo-gallery-title span").text(data.description)
					
					$.each(data.photoset.photo, function(i,item){
						
							
							//var sourceSquare = (item.media.m).replace("_m.jpg", "_s.jpg");
							
							//var sourceLarge = (item.media.m).replace("_m.jpg", ".jpg");
							
							$("#photo-gallery-cont").append("<li><a href='"+ item.url_m +"' title='"+ item.title+ "'><img src='"+ item.url_sq +"'></a></li>")
							
						
						
					  });
				  
				 	//instanciate lightbox
					$("#photo-gallery-cont a").lightBox(
								{
									imageLoading: 'http://selkirkarmshotel.co.uk/images/lightbox-ico-loading.gif',
									imageBtnClose: 'http://selkirkarmshotel.co.uk/images/lightbox-btn-close.gif',
									imageBtnPrev: 'http://selkirkarmshotel.co.uk/images/lightbox-btn-prev.gif',
									imageBtnNext: 'http://selkirkarmshotel.co.uk/images/lightbox-btn-next.gif'
								}
							);
				  
				});
				
				} else {
				
					return false;
				
				}
		
		
		},
		
		releaseTabs : function(e){
			
			if (e != undefined) {
					e.preventDefault();
				}
			
			$("#photo-loader").show();
			$("#release-cont").hide().html('');
			
			if (firstTab != '') {
				var tab = firstTab
			} else {
				var tab = $(this).attr("rel");
			}
			
			//console.log(tab)
			
			var sUrl = "http://www.chimaira.com/the-infection/wp-content/themes/chimaira09/"+tab+".php";
			
			$("#release-cont").load(sUrl, function(){
				
					$("#release-cont .lb").lightBox(
								{
									imageLoading: 'http://selkirkarmshotel.co.uk/images/lightbox-ico-loading.gif',
									imageBtnClose: 'http://selkirkarmshotel.co.uk/images/lightbox-btn-close.gif',
									imageBtnPrev: 'http://selkirkarmshotel.co.uk/images/lightbox-btn-prev.gif',
									imageBtnNext: 'http://selkirkarmshotel.co.uk/images/lightbox-btn-next.gif'
								}
							);
				
					firstTab = '';
					$("#photo-loader").hide();
					$("#release-cont").show();
				
			});
			
			
			
		},
		
		tourMap : function(){
			
			var gmap = $("a.gigpress-address").text();
			
			gmap = gmap+" "+mapAdd;
			
			
			//console.log(gmap)
			
			$("#map-cont").googlemap({addresses: [gmap], zoom: 15, labels : true, controls: true });
			
		
		},
		
		tourTable : function(e){
			
			var sUrl = $(this).attr("title");
			
			document.location.href = sUrl;
			
		},
		
		tourTableToggle : function(e){
			e.preventDefault();
			
			
			if($(this).hasClass("tour-toggle")){
					
					$(this).attr("class","tour-toggle-on")
					
				}else{
					
					$(this).attr("class","tour-toggle")
				}
			
			$(this).parent().parent().parent().find(".gigpress-info").children().children(".td-cont").slideToggle("fast");

			
		}
		
		
			
	 }

}();

// load on document ready
$(function() {
	chimairaGlobalJS.init();
})

