function fbs_click() {}

var Gallery = {
	imageGallery : null,
	page : 0,
	page_count : 0,
	iframe_reload_time: 90000,
	
	image_id : null,
	gallery_id : null,
	
	gallery : null,
	
	ad_page : 10,
	ad_viewed : false,
	ad_img : '/client/v2/img/temp/midori_pic.jpg',
	

	init: function() {
		if (Gallery.gallery_id == 6082) {
			$('.image_holder').css({background : '#000'});
		}	
		
		$('div.image_holder').append('<img src="" alt="" id="gallery_image" />');
		$('img#gallery_image').click(function(){
			if ($('img#gallery_image').attr('src') == Gallery.ad_img) {
				Gallery.openUrl("/sweetkiss/");
			}
			Gallery.showNextImageGalleryPage();
		});
		this.initNextPagination();
		this.initPreviousPagination();
		
		$('#gallery_article').hide();
		$('select#image_linking_list').select(function() {
			Gallery.openUrl("/userPresentation/?uid=" + $(this).val())
		});
		$('a#mms_image_button').click(function() {
			var url = 'http://app.addmob.com/customers/finest/web/Finest_webpage.aspx?Image=';
			$.openPopup(url + escape(Gallery.imageGallery.images[Gallery.page - 1]['url']), 'objectWin', 716, 780, false);
			return false;
		});
		$('a#facebook_button').click(function() {
			var u='http://www.finest.se/imageGallery/image.php?igid=' + Gallery.gallery_id + '&iid=' + Gallery.image_id;
			var t=document.title;
			window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
			return false;
		});
		$('a#messenger_button').click(function() {
			var u='http://www.finest.se/imageGallery/image.php?igid=' + Gallery.gallery_id + '&iid=' + Gallery.image_id;
			var t=document.title;
			window.open('http://profile.live.com/badge?url='+encodeURIComponent(u),'sharer','toolbar=0,status=0,width=1000,height=600');
			return false;
		});
		$('a#add_image_to_profile_button').click(function() {
			Gallery.addUserToCurrentImageLinkers();
			return false;
		});
		$('a#view_more_galleries').click(function() {
			Gallery.toggleLatestGalleries();
			return false;
		});
		
		_gaq.push(['_trackEvent', 'Gallery', 'View gallery', $('#gallery_name').text()]);
		
		setTimeout('Gallery.reloadIframe()', Gallery.iframe_reload_time);
	},
	toggleLatestGalleries: function() {
		if ($('div#more_galleries_holder').css('right') == '0px') {
			// $('div#more_galleries_holder').css("right", "-127px");
			$('div#more_galleries_holder').animate({ right: "-127px" }, 500 );
		} else {
			// $('div#more_galleries_holder').css("right", "0px");
			$('div#more_galleries_holder').animate({ right: "0px" }, 500 );
		}
	},
	initNextPagination: function() {
		$('ul.pagination li.next a').click(function() {
			Gallery.showNextImageGalleryPage();
		});
	},
	initPreviousPagination: function() {
		$('ul.pagination li.previous a').click(function() {
			Gallery.showPreviousImageGalleryPage();
		});
	},
	showImageGalleryPage: function() {
		if(Gallery.page == 0) {
			$('div.image_holder').hide();
			$('#gallery_article').show();
			$('#image_description').text('');
		} else if(Gallery.page > 0) {
			$('div.image_holder').show();
			$('#gallery_article').hide();
			$('img#gallery_image').hide();
			Gallery.image_id = this.imageGallery.images[this.page - 1]['id'];	
			
			if (this.imageGallery.images[this.page - 1]['sweetkiss'] == 1) {
				$('#midori').fadeIn();	
			} else {
				$('#midori').fadeOut();	
			}
			
			var type = Gallery.imageGallery.images[Gallery.page - 1]['url'].split('.');
			type = type[type.length-1];
			$('#gallery_flash_holder').hide();

			if (type == 'swf') {
				$('#gallery_flash_holder').fadeIn();
				$('#image_description').html(Gallery.imageGallery.images[Gallery.page - 1]['description']);
				var so = new SWFObject(Gallery.imageGallery.images[Gallery.page - 1]['url'], "gallery_flash", "728", "484", "8", "#000000");
				pageTracker._trackPageview("/philips/view");
				so.write("gallery_flash_holder");
			} else {
				$('img#gallery_image').attr('src', Gallery.imageGallery.images[Gallery.page - 1]['url']).load(function() {
					$('#image_description').html(Gallery.imageGallery.images[Gallery.page - 1]['description']);
					$(this).fadeIn();
				});
			
			}
			
			
			/* Make sure image is loaded by preloading it 
			$('img#gallery_image').attr('src', Gallery.imageGallery.images[Gallery.page - 1]['url']).load(function() {
				$('#image_description').html(Gallery.imageGallery.images[Gallery.page - 1]['description']);
					$(this).fadeIn();
			});*/
		}
		
		Gallery.gallery_id = this.imageGallery['id'];
		
		$('#current_page').text(this.page);
		if (this.page == this.page_count) {
			this.toggleLatestGalleries();
		} else {
			Gallery.preloadImage(Gallery.imageGallery.images[Gallery.page]['url']);
		}

		Gallery.refreshCurrentImageLinkingUsersList();
		
		
		if (Gallery.page != 0) {
			_gaq.push(['_trackEvent', 'Gallery', 'View image', $('#gallery_name').text() + ', sida ' + Gallery.page]);
			//pageTracker._trackPageview("/imageGallery/gallery_popup_image.php?igid=" +  Gallery.gallery_id + "&iid=" + Gallery.image_id + "&pageNum=" + Gallery.page);
		}
		
	},
	preloadImage : function(img1, img2) {
		$.preloadImages(img1, img2);
	},
	loadImageGallery : function() {
		
		$.getJSON("/json/getImageGallery.php", {"igid" : Gallery.gallery_id}, function(data){
				Gallery.imageGallery = data;
				Gallery.page_count = Gallery.imageGallery.images.length;
				Gallery.showImageGalleryPage(this.page);
				Gallery.checkPagination();
				
				$('#page_count').text(Gallery.page_count);
			});
	},
	checkPagination : function() {
		if (this.page > 0 && $('ul.pagination li.previous span span').size() > 0) {
			$('ul.pagination li.previous').html('<a href="#"><span>Föregående</span></a>');
			this.initPreviousPagination();
		}		
		if (this.page == this.page_count) {
			$('ul.pagination li.next').html('<span><span>Nästa</span></span>');
		}
		if (this.page < this.page_count && $('ul.pagination li.next span span').size() > 0) {
			$('ul.pagination li.next').html('<a href="#"><span>Nästa</span></a>');
			this.initNextPagination();
		}
		if (this.page == 0) {
			$('ul.pagination li.previous').html('<span><span>Föregående</span></span>');
		}	
	},
	showAd : function() {
		$('img#gallery_image').attr('src', Gallery.ad_img).load(function() {
			$(this).fadeIn();
			$('#image_description').html('Midori Sweet Kiss');
		});
	},
	
	showNextImageGalleryPage : function() {
		/*if (!Gallery.ad_viewed && parseInt(Gallery.page)+1 == Gallery.ad_page) {
			Gallery.ad_viewed = true;
			Gallery.showAd();
			return;
		}*/
		
		if (this.page < this.page_count) {
			Gallery.page++
		}
		this.checkPagination();
		Gallery.showImageGalleryPage();
	},
	
	showPreviousImageGalleryPage : function() {
		/*if (!Gallery.ad_viewed && Gallery.page-1 == Gallery.ad_page) {
			Gallery.ad_viewed = true;
			Gallery.showAd();
			return;
		}*/
		
		if (this.page > 0) {
			Gallery.page--
		}
		this.checkPagination();
		Gallery.showImageGalleryPage();
	},
	refreshCurrentImageLinkingUsersList : function() {
		var list = $('select#image_linking_list').html('');

		if(Gallery.page == 0) return;

		var linkingUsers = this.imageGallery.images[this.page - 1]['linkingUsers'];
		list.append('<option value="">Tillagd i profil av</option>');
		
		for(var i = 0; i < linkingUsers.length; i++) {
			var user = linkingUsers[i];
			list.append('<option value="' + user['id'] + '">' + user['username'] + ", (" + (user['male'] ? "M" : "K") + user['age'] + ")" + '</option>');
		}

		/* Update linking button */
		if (this.imageGallery.images[this.page - 1]['canAddImageLinkToLoggedInUser']) {
			$('li#add_image_to_profile_holder').show();
		} else {
			$('li#add_image_to_profile_holder').hide();
		}
		
	},		
	addUserToCurrentImageLinkers : function() {
		$.get("/json/getImageGallery.php", {
				igid: this.gallery_id, 
				addImageLinkToLoggedInUser: Gallery.imageGallery.images[Gallery.page - 1]['id']
			}, function (data) {
				Gallery.refreshCurrentImageLinkingUsersList()
			}
			
		);		
	},
	reloadIframe : function() {
		$('iframe').each(function(i) {
			this.contentWindow.location.reload(true);
			if (i+1 == $('iframe').size()) {
				setTimeout('Gallery.reloadIframe()', Gallery.iframe_reload_time);
			} 
		});
	},
	openUrl : function(url) {
		window.opener.location = url;
	}
}

function init() {
	var gallery_page = $('body').attr('id').split('_');
	Gallery.gallery_id = gallery_page[1];
	Gallery.page = gallery_page[2];

	Gallery.init();
	
	Gallery.loadImageGallery();
}


$(document).ready(init);
