$(document).ready(function(){
	
	// If the mailing list module is loaded on the page, enable the submit button
	$('a#mailinglist_submit').click(function(){ mailinglist_subscribe(); return false; });
});

function openShadowbox(content, player, title){
    Shadowbox.open({
        content:    content,
        player:     player,
        title:      title
    });
}


function open_gallery(gallery_id){
	
	$.getJSON("../../gallery.php?id="+gallery_id,
	        function(data){
		
				var options = {
			        continuous: true
			    };
			   
				var images = [];
				j = data.images.length
				for (var i=0; i < j; i++) {
					images.push({ 
						player:     data.images[i].display_type,
				        content:    data.images[i].filename,
				        height:     data.images[i].height,
				        width:      data.images[i].width,
				        options:    options,
				  		title:     data.images[i].title
				    });
				};
	
	      		Shadowbox.open(images);
	        });

}

function changeLanguage(newLanguage){
	
		// Get the current URL
		sURLFull = window.location.href.split("?");
	
		// Grab the script path.
		sURL = sURLFull[0]+'?language='+newLanguage;
	
		// If there was a query string attached to the current page load...
		if(sURLFull.length > 1 && sURLFull[1].length>0){
			// ... replace any language setting and add it to the new URL.
			sURL += "&" + sURLFull[1].replace('language','old');
		}
		
	// Reload the window with the new language.
	window.location.href = sURL;
}

function mailinglist_subscribe(){
	email = $('input#mailinglist_email').val();
	name =  $('input#mailinglist_name').val();
	
	data = {};

	
	data.email = $('input#mailinglist_email').val();
	data.name = $('input#mailinglist_name').val();
	
	$(".mailinglist_custom input:selected").each(function(i){
		console.log($(this).attr('id') + " :: " + $(this).val());
	});
	
	$(".mailinglist_custom input:checked").each(function(i){
		input_name = $(this).attr('name');
		input_value =  $(this).val();
		
		if(data[input_name] == undefined){
			data[input_name] = input_value;		
		} else {
			data[input_name] += ","+input_value;
		}

	});

	$(".mailinglist_custom input:text").each(function(i){
		if(data[input_name] == undefined){
			data[input_name] = input_value;		
		} else {
			data[input_name] += ","+input_value;
		}
	});
	
	$.post(site_url+"/"+language+"/ajax/mailinglist/", 
			data,
	   		function(data){	
			     console.log(data); //  2pm
			});	
			
	
}