var ps;

pastan = function() {
	var ps = this; // for use in events
	ps.ajax_url = '/wp-content/themes/pastan/ajax.php';
	ps.current_category = 0;
	ps.current_list = "";
	ps.store_list_html = '<div class="row"><div class="col title">&nbsp;</div><div class="col phone">&nbsp;</div><div class="col address">&nbsp;</div><div class="col www"><a href="#" target="_blank">&nbsp;</a></div><br class="clear" /><div class="entry"><div class="map left"><!-- --></div><div class="info left"><p><strong>Adress</strong><br /><span class="address1">&nbsp;</span><br /><span class="address2">&nbsp;</span></p><p><strong>Telefon</strong><br /><span class="phone">&nbsp;</span></p><p><strong>Hemsida</strong><br /><a href="#" class="www"  target="_blank">&nbsp;</a></p></div><br class="clear" /></div></div>';
	ps.maps = new Array();
	ps.tip_sent = false;
	
	ps.construct = function() {	
		// show possible children in the main menu
		$('#wrapper #header #hline ul li').bind("mouseover", function() {
			if($(this).find('ul').length) {
				$(this).find('ul').show();
			}
		}).bind("mouseout", function() {
			if($(this).find('ul').length) {
				$(this).find('ul').hide();
			}
		});	
	
		// right column
		$("#right-col .accordion").accordion({autoHeight: false});
		$("#right-col .accordion h3:first").addClass('first');
		
		// store categories and list
		if($('#store-categories').length) {
			// when a visitor clicks on one of the main categories
			$('#store-categories ul:first li a').click(function() {
				// exit if already loading or parent is the same
				if($('#store-categories .loader:visible').length || $(this).attr('href') == ps.current_category) {
					return;
				}
				
				ps.current_category = $(this).attr('href');
				
				// reset the links
				$('#store-children ul li').each(function() {
					$(this).removeClass("active")
					$(this).find('a').attr("href", "#").html("&nbsp;");
				});
				
				// show the loader
				$('#store-categories .loader').toggle();
				
				// make the link active
				$('#store-categories ul:first li').each(function() {
					$(this).removeClass('active');
				});
				
				$(this).parent().addClass('active');
				
				$.post(ps.ajax_url, {action: "get_categories", parent: $(this).attr('href')}, function(data, status) {
					if(status != "success") {
						alert('Failed!');
						return;
					}
					
					$('#store-categories .loader').toggle();

					$.each(data, function(key, item) {
						$('#store-children ul li:eq('+ key +') a').html(item.name);
						$('#store-children ul li:eq('+ key +') a').attr('href', item.term_id);
					});
				}, "json");
			});
			
			// when clicking on a category child (subcategory)
			$('#store-children ul li a').click(function() {
				// clear old data
				$('#store-list .body').html("");
				
				$('#store-list-loader').show();
				$('#store-list-wrapper h2').text($(this).text());
				
				$('#store-list-wrapper').slideDown("fast");
				
				// get posts from a specific category
				$.post(ps.ajax_url, {action: "get_posts", category_id: $(this).attr('href')}, function(data, status) {
					if(status != "success") {
						alert('Failed!');
						return;
					}
					
					// add rows and it's data
					$.each(data, function(key, item) {
						$('#store-list .body').append(ps.store_list_html);

						var row = $('#store-list .body .row:eq('+ key +')');
						
						$(row).find('.col.title').text(item.title);
						$(row).find('.col.phone').text(item.phone);
						$(row).find('.col.address').text(item.address1);
						
						if(item.www.length > 4 && item.www.substr(0, 4) != "http")
							item.www = 'http://'+ item.www;
						
						$(row).find('.www a').text(item.www).attr('href', item.www);
							
						// set the more detaild information
						$(row).find('.entry .address1').text(item.address1);
						$(row).find('.entry .address2').text(item.address2);
						
						$(row).find('.entry .phone').text(item.phone);
						$(row).find('.entry .www').text(item.www).attr('href', item.www);
						
						//ps.maps[key] = new gmaps($('#store-list .body .row:eq('+ key +') .entry .map'), 62.390228, 17.308499, 15, 424, 349);
						
						//if(item.address1 != "")
							//ps.maps[key].add_marker(item.address1 +", Sundsvall", true, 16);
					});
					
					$('#store-list-wrapper .loader').hide();
					$('#store-list').slideDown("fast");

					// when clicking on a row in the store list
					$('#body #store-list .row .col.title').click(function() {
						if(ps.current_list == this)	
							return
							
						ps.current_list = this;
					
						var map = new gmaps($(this).parent().find('.entry .map'), 62.390228, 17.308499, 15, 424, 349);
						
						var address = $(this).parent().find('.entry .address1').text();

						if(address != "")
							map.add_marker(address +", 852 32, Sundsvall", true, 16);
						
						map = null;
						
						$('#body #store-list .row .entry').hide("fast");
						$('#body #store-list .row').removeClass("active");
						
						$(this).parent().find('.entry').toggle("fast");
						$(this).parent().addClass("active");
					});
				}, "json");
			});			
		}
	}
	
	ps.send_tips = function() {
		if(ps.tip_sent == true) {
			$('#tips-form .message').html("Du har redan skickat<br />ett meddelande.");
			return;
		}
		
		if($('#textarea-message').val() == "") {
			$('#tips-form .message').html("Du måste skriva ett meddelande.");
			return;
		}
		
		ps.tip_sent = true;
		
		$('#tips-form .message').text("Skickar...");
		$.post(ps.ajax_url, {action: "send_tip", name: $('#input-name').val(), email: $('#input-email').val(), message: $('#textarea-message').val()}, function(data, status) {
			$('#tips-form .message').text("Tack för din hjälp.");
			
			$('#input-name').val("");
			$('#input-email').val("");
			$('#textarea-message').val("");
		}, "json");
	}
	
	ps.submit = function(form) {
		if(typeof form == "undefined")
			return;
		
		$(form).submit();
			
		return true;
	}
	
	ps.construct();
}

// initialize class
$(document).ready(function() {
	ps = new pastan();
	
    var parking = new gmaps('#parking-map');
	parking.add_parkings();
});

var addthis_config = {
     ui_cobrand: "PåStan",
	 services_compact: 'email, facebook, twitter'
}
