/*
    fShow ver 0.2 - make your own Flickr embed slideshows
    Copyright (C) 2007  Radosław Piekarz (http://radziu.jogger.pl)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
$(document).ready(function() {
		var userId;
		var newSet;
		var tagsArray;
		/*---   Creates slideshow html codes   ---  */
		$("#createCode").click(function()
			{
				var style;
				$("#step4//input[@type=radio][@checked]").each(function()
				{
					style =  ($(this).attr('value'));
				});
				
				newSet = (newSet === undefined)? '' : newSet;
				//for  IE 6.0
				newSet = newSet.replace('http://fshow.ovh.org/', '');
				var tags = $('#tags').val();
				var width = $('#width').val(); 
				var height = $('#height').val();
				var text = $('#fullText').val();
				last = tags.substr(tags.length-1, 1);
				if(last == ',')
				{
					tags = tags.substr(0, tags.length-1);
				}
				if(style == 'old')
				{
					var iframe = '<iframe src="http://www.flickr.com/slideShow/index.gne?user_id='+userId+'&set_id='+newSet+'&tags='+tags+'&text='+text+'" width="'+width+'" height="'+height+'" scrolling="no" frameborder="0"></iframe>';
					var object = '<object type="text/html" data="http://www.flickr.com/slideShow/index.gne?user_id='+userId+'&set_id='+newSet+'&tags='+tags+'&text='+text+'" width="'+width+'" height="'+height+'">Your browser doesn\'t support embeded html pages</object>';
				}
				else
				{
					if (newSet != '')
					{
						var iframe = '<iframe src="http://www.flickr.com/photos/'+userId+'/sets/'+newSet+'/show" width="'+width+'" height="'+height+'" scrolling="no" frameborder="0"></iframe>';
						var object = '<object type="text/html" data="http://www.flickr.com/photos/'+userId+'/sets/'+newSet+'/show" width="'+width+'" height="'+height+'">Your browser doesn\'t support embeded html pages</object>';
					}
					else
					{
						var tag = tags.substr(0, tags.length - tags.indexOf(",") - 1);
						var iframe = '<iframe src="http://www.flickr.com/photos/'+userId+'/tags/'+tag+'/show" width="'+width+'" height="'+height+'" scrolling="no" frameborder="0"></iframe>';
						var object = '<object type="text/html" data="http://www.flickr.com/photos/'+userId+'/tags/'+tag+'/show" width="'+width+'" height="'+height+'">Your browser doesn\'t support embeded html pages</object>';
					}
				}
				$('#iframe').val(iframe);
				$('#preview').empty();
				$('#preview').append(iframe);
				$('#object').val(object);
				/*Saving stats*/
				params = 'action=newSlideShow';
				var stats = $.ajax(
				{
					type: "POST",
					url: "stats.php",
					dataType: "xml",
					data: params
				});
			});
			/*---   Shows  user's tags   ---  */
			$('#showTags').click(function()
			{
				$(this).text();
				var img = $('#showTagsImg').attr("src");
				if(img == 'img/arrow_down.png')
					{
					$(this).html('<img src="img/arrow_up.png" id="showTagsImg"/>Hide tags');
					$('#tagsS').show('fast');
					}
				else
					{
					$(this).html('<img src="img/arrow_down.png" id="showTagsImg />Show tags');
					$('#tagsS').hide('fast');
					}
			return false;
			});
			/*---   Showes  user's sets   ---  */
			$('#showSets').click(function()
			{
				$(this).text();
				var img = $('#showSetsImg').attr("src");
				if(img == 'img/arrow_down.png')
					{
					$(this).html('<img src="img/arrow_up.png" id="showSetsImg"/>Hide sets');
					$('#sets').show('fast');
					}
				else
					{
					$(this).html('<img src="img/arrow_down.png" id="showSetsImg />Show sets');
					$('#sets').hide('fast');
					}
			return false;
			});
			/*---   Loads   user's data   ---  */
			function loadData()
			{
				params = $('#username').serialize();
				$("#usernameNext").attr({src: "img/loading.gif"});
				var test = $.ajax(
				{
					type: "POST",
					url: "ajax.php",
					dataType: "xml",
					data: params,
					success: function(xml)
						{
							var html = "";
							var js = "";
							$(xml).find('user_id').each(function()
							{
								userId = $(this).text();
							});
							$(xml).find('step2').each(function()
							{
								var item_text = $(this).text();
								$('#sets').empty();
								$('#sets').prepend(item_text);
								$('#step2').show('slow');
							});
							$(xml).find('step3').each(function()
							{
								var item_text = $(this).text();
								$('#tagsS').empty();
								$('#tagsS').prepend(item_text);
								$('#step3').show('slow');
								$('#step4').show('slow');
								$('#step5').show('slow');
							});
							$(xml).find('js').each(function()
							{
								var item_text = $(this).text();
								$('#footer').prepend(item_text);
							});
							$("#usernameNext").attr({src: "img/accept.png"});
								$("a.addTag").each(function(i)
								{
									$(this).click(function()
									{

										var text = $('#tags').val();
										var old = text.indexOf($(this).text());
										if(old >=0)
										{
											var toChange = $(this).text() + ',';
											text = text.replace(toChange, '');
											$('#tags').val(text);
										}
										else
										{
											var newTag = $('#tags').val()  + $(this).text() + ',';
											$('#tags').val(newTag);
										}
										$(this).toggleClass('selectedTag');
										return false;
									});
								}
								);
								$("a.chooseSet").each(function(i)
								{
									$(this).click(function()
									{

										if(newSet !== $(this).attr("href"))
										{
											newSet = $(this).attr("href");
										}
										else
										{
											newSet = null;
										}
										$(this).toggleClass('selectedSet');
										$("a.chooseSet").each(function(i)
										{
											if($(this).attr('href') != newSet)
											{
												$(this).removeClass('selectedSet');
											}
										});
										return false;
									});
								}
								);
							
						}
				});
				return false;
			}
			/*---   Checks  username and loads data   ---  */
			$("#usernameNext").click(function()
			{
				if($('#username').val().length > 0)
				{
					loadData();
				}
				else
				{
				alert('Please write username');
				}
			});
			/*---   Checks  username and loads data   ---  */
			$("#username").keypress( function(e) 
			{
				if (e.keyCode == 13)
				{
					if($('#username').val().length > 0)
					{
						loadData();
					}
					else
					{
						alert('Please write username');
					}
				}
				
			});
		
		});