
var backgroundImageAttr;

$(document).ready(function() {
	loadGallerySlider();
	
	// Toogle post
	$('div.post div.title').live('click', togglePost);
	
	// Toogle between stores and trademarks
	$('#sidebar div.tabs a').click(toggleSidebarTabs);
	
	// Toggle categories
	$('#sidebar div.box a.category').click(toggleSubCategories);
	
	// Toggle trademakrs
	$('#sidebar div.box.trademarks a.trademark-category').click(toggleTrademarks);
	
	// Toggle stores in trademark
	$('#sidebar div.box.trademarks a.sub-category').click(toggleTrademarkStores);
	
	// Observe click for ajax reload
	$('#sidebar div.box.stores a.sub-category, #sidebar div.box.foods a.sub-category, #sidebar div.box.information a.category, #sidebar div.box a.language, #sidebar div.box.opening a, #sidebar div.box a.calendar').click(loadPage);
	$('#sidebar div.box.trademarks div.trademark-stores a').live('click', loadPage);
	
	$('body.red #sidebar a.category, body.red #sidebar .letters a.trademark-category, body.red #sidebar .tabs a').hover(
		function(){
			var url = $('img', this).attr('src');
			url = url.replace( '/img/menu/' , '/img/menu-white/' );
			$('img', this).attr('src', url);
		},
		function(){
			var url = $('img', this).attr('src');
			url = url.replace( '/img/menu-white/' , '/img/menu/' );
			$('img', this).attr('src', url);
		}
	);
	
	// Get background image
	var background = $('img.background');
	
	backgroundImageAttr = {
        natural_width: background.attr('width'),
		natural_height: background.attr('height')
    };
	
	background.removeAttr('width').removeAttr('height');
	
	// Observe window resize and crop image
	$(window).resize(backgroundCrop);
	
	// Crop background
	backgroundCrop();
});

loadGallerySlider = function(id) {
	if (id == '') {
		// Loop through all image galleries
		$('div.image_gallery').each(function(i, d) {
			// Save object
			var image_gallery = $(d);
		
			// Find images
			var images = image_gallery.find('div.images');
		
			$('#' + images.attr('id')).easySlider({
				auto: true, 
				continuous: true,
				prevText: '',
				nextText: ''
			});
		});
	} else {
		$('#' + id).easySlider({
			auto: true, 
			continuous: true,
			prevText: '',
			nextText: ''
		});
	}
}

togglePost = function() {
	var post = $(this);
	
	post.toggleClass('active');
	
	if (!post.hasClass('active')) {
		$('div.left_arrow, div.right_arrow').fadeOut();
	}
	
	// Display content
	post.next('div.content').stop(true, true).slideToggle('normal', function() {
		if (post.hasClass('active')) {
			$('div.image_gallery div.left_arrow, div.image_gallery div.right_arrow').fadeIn();
		}
		
		var image_gallery = $(this).find('div.image_gallery');
		
		if (image_gallery.length > 0) {
			loadGallerySlider(image_gallery.find('div.images').attr('id'));
		}
	}).toggleClass('hidden');
}

toggleSidebarTabs = function() {
	// Get clickes object
	var tab = $(this);
	
	// Get image in object
	var img = $(this).find('img');
	
	// Get imagess
	var trademark_img = $('#sidebar div.tabs img.trademarks');
	var shop_img = $('#sidebar div.tabs img.shops');
	
	// Remove class from both tabs
	$('#sidebar div.tabs a').removeClass('active');
	
	// Add class to clicked tab
	tab.addClass('active');
	
	if (img.attr('class') == 'shops') {
		$('#sidebar div.box.trademarks').addClass('hide');
		$('#sidebar div.box.stores').removeClass('hide');
		
		shop_img.attr('src', shop_img.attr('src').replace('-2', '-1'));
		trademark_img.attr('src', trademark_img.attr('src').replace('-1', '-2'));
	} else {
		$('#sidebar div.box.stores').addClass('hide');
		$('#sidebar div.box.trademarks').removeClass('hide');
		
		trademark_img.attr('src', trademark_img.attr('src').replace('-2', '-1'));
		shop_img.attr('src', shop_img.attr('src').replace('-1', '-2'));
	}
}

toggleSubCategories = function() {
	var category = $(this);
	
	// Remove class from all categories
	$('#sidebar div.box a.category').removeClass('active');
	
	if (!category.next('div.sub-categories').is(':visible')) {
		// Slide up all active sub categories
		$('div.sub-categories').slideUp();
		
		// Add class for category
		category.addClass('active');
	
		// Toggle sub categories
		category.next('div.sub-categories').stop(true, true).slideDown();
	} else {
		// Slide up all active sub categories
		$('div.sub-categories').slideUp();
	}
}

toggleTrademarks = function() {
	var trademark = $(this);
	
	// Get selected object
	var current_trademark = $('#category-trademarks-' + trademark.attr('alt'));
	
	if (current_trademark.is(':visible')) {
		show = false;
	} else {
		show = true;
	}
	
	// Remove class from all letters
	$('#sidebar div.box.trademarks a.trademark-category').removeClass('active');
    
    // Hide all trademarks
	$('#sidebar div.box.trademarks div.sub-categories').slideUp();
    
	if (show) {
    	// Toggle class for trademark category
    	trademark.addClass('active');
	
		// Show selecleted letter
		current_trademark.stop(true, true).slideDown();
	}	
}

toggleTrademarkStores = function() {
	var page = $(this);
	
	// Remove class on all sub categories
	$('a.sub-category').removeClass('active');
	
	// Set active class
	page.addClass('active');
	
	// Get href
	var href = page.attr('href');
	
	// Slide up trademark stores
	$('div.trademark-stores').slideUp('normal', function() {
		$(this).remove();
	});
	
	$.ajax({
		url: href,
		async: 'true',
		cache: 'true',
		data: 'ajax=true',
		type: 'post',
		dataType: 'json',
		success: function(data) {
			var html = '';
			
			$.each(data, function(i, o) {
				//html += '<a href="' + o.url + '" class="trademark-store"><img src="' + o.img + '" /></a>';
				html += '<a href="' + o.url + '" class="trademark-store">' + o.name + '</a>';
			});
			
			// Create div after page
			page.after('<div class="trademark-stores">' + html + '</div>');
			
			$('div.trademark-stores').slideDown();
		}
	});
	
	return false;
}

loadPage = function() {
	var page = $(this);
	
	if (!page.hasClass('trademark-store')) {
		// Remove class on all sub categories
		$('a.sub-category').removeClass('active');
		
		// Set active class
		page.addClass('active');
	}
	
	// Get href
	var href = page.attr('href');
	
	// Slide up ajax content handler
	$('#ajax-post').slideUp('normal');
	
	$.ajax({
		url: href,
		async: 'true',
		cache: 'true',
		data: 'ajax=true',
		type: 'post',
		dataType: 'html',
		success: function(html) {
			$('#ajax-post').html(html).slideDown('normal');
		}
	});
	
	return false;
}

changeImage = function(direction, obj) {
	// Get container for all images
	var image_container = $(obj).parent();
	
	// Get images
	var images = image_container.find('img');
	
	var current_image = 0;
	var image_count = images.length;
	
	// Loop trough images
	$.each(images, function(i, d) {
		// If image doesnt have hidden class, that is current image
		if (!$(d).hasClass('hidden')) {
			current_image = i;
			$(d).fadeOut('fast', function() {
				$(d).addClass('hidden');
				
				// If first image and direction is previous
				if (current_image == 0 && direction == 'prev') {
					next_image = image_count - 1;
				// Else if last image and direction is next
				} else if (current_image == (image_count - 1) && direction == 'next') {
					next_image = 0;
				// Else if previous image
				} else if (direction == 'prev') {
					next_image = current_image - 1;
				// Else next image
				} else {
					next_image = current_image + 1;
				}

				// Hide all images
				//images.addClass('hidden');

				// Show next image
				$.each(images, function(i, d) {
					if (i == next_image) {
						$(d).fadeIn('fast', function() {
							$(d).removeClass('hidden');
						});
						
						return;
					}
				});
			});
		}
	});
}

changeToImage = function(obj, src) {
	// Remove active class
	$('div.small_images img').removeClass('active');
	
	// Add to current
	$(obj).addClass('active');
	
	$('div.image_gallery_small div.big_image img').attr('src', src);
}

backgroundCrop = function() {
    var img = $("img.background");
    if (img)
    {
        var window_width = $(window).width();
        var window_height = $(window).height();
        ratio_window = window_height / window_width;
        ratio_img = backgroundImageAttr.natural_height / backgroundImageAttr.natural_width;

        if (ratio_window > ratio_img)
        {
            new_width = window_height / ratio_img;
            img.css("height", window_height + "px");
            img.css("width", "auto");
            img.css("left", "-" + parseInt((new_width - window_width)/2) + "px");
        }
        else
        {
            img.css("width", "100%");
            img.css("height", "auto");
            img.css("left", "0");
        }
    }
}

var facebook	= "http://www.facebook.com/sharer.php?u=";
var twitter		= "http://twitter.com/home?status=";

share = function(where, url, title){
	var url;
	
	if (where == "facebook") {
		url = facebook + url + "&t=" + title;
	} else if (where == "twitter") {
		url = twitter + url;
	}
	
	window.open(url, "share", "toolbars=0, scrollbars=0, location=0, statusbars=0, menubars=0, width=550, height=330");
}

function sendContactForm() {
	var error_msg = "";
	
	if ($('#first_name').val().length == 0) {
		error_msg = "\n - Förnamn";
	}
	
	if ($('#last_name').val().length == 0) {
		error_msg += "\n - Efternamn";
	}
	
	if ($('#email').val().length == 0) {
		error_msg += "\n - E-postadress";
	}
	
	if ($('#phone').val().length == 0) {
		error_msg += "\n - Telefonnummer";
	}
	
	if ($('#title').val().length == 0) {
		error_msg += "\n - Rubrik";
	}
	
	if ($('#message').val().length == 0) {
		error_msg += "\n - Meddelande";
	}
	
	if (error_msg != "") {
		alert("Du måste fylla i följande fält:" + error_msg);
	} else {
		$('#contact_form').submit();
	}
}
