$(document).ready(function()
{
 if($("a[rel^='prettyPhoto']").length>0)
   $("a[rel^='prettyPhoto']").prettyPhoto();

 if($("a.imgPreview").length>0){
 	$("a.imgPreview").imgPreview({
 		containerID:'imgPreviewWithStyles',
 		srcAttr:'rel'
 	});
 }
 
 if($('.imageRotator').length>0){
  		
  		$('.imageRotator').hide();
  		$.get("/includes/loadTopPhotos.php?author="+author, function(data){
  	    $(data).appendTo('.imageRotator');
  	    $('.imageRotator').cycle({fx: 'fade'});
 		
 		});
	    $('.imageRotator').show();

  }
  
  if($('#fileInput').length>0){
  
        $('#fileInput').show();
  		$('#fileInput').uploadify({
									'uploader'  : 'uploadify.swf',
									'script'    : '/upload_photos.php',
									'cancelImg' : '/gfx/cancel.png',
									'multi'		: 'true',
									'onComplete': function(event, queueID, fileObj, response, data){
										$("#uploaded").append('<img src="/show_thumb.php?'+fileObj.name+'" align="left" width="100"><br/>');
									}
  		});
  		
  		
  		$("#uploadButton").click(function(){
  			
  			var hash = $("#PHPSESSID").val();
  			var rootid = $("#root_id").val();
  			
  			$('#fileInput').uploadifySettings('scriptData', {'root_id' : rootid, 'PHPSESSID' : hash});
  			$('#fileInput').uploadifyUpload();
  		});
  }
  
  if($("#privGalSelect1").length>0){
  	$("#privGalSelect1").click(function(){
  		$("#privateGall").hide();
	});
  }

  if($("#privGalSelect2").length>0){
  	$("#privGalSelect2").click(function(){
  		$("#privateGall").show();
	});
  }
  
  if($(".tagme").length>0){
  		$('.tagme').photoTag({
		requesTagstUrl: '/static/photo-tag/tests/photo_tags/photo-tags.php',
		deleteTagsUrl: '/static/photo-tag/tests/photo_tags/delete.php',
		addTagUrl: '/static/photo-tag/tests/photo_tags/add-tag.php',
		parametersForNewTag: {
			name: {
				parameterKey: 'name',
				isAutocomplete: true,
				autocompleteUrl: '/static/photo-tag/tests/photo_tags/names.php',
				label: 'Name'
			}
		}
	});
  }
  
  if($("form#postComment").length>0){
  
  	$("form#postComment").submit(function(){
        var page = $(this).attr("action");
		var	req = $(this).serialize();
		var url = page+'?api=ajax&'+req;
		$.get(url, function(data) {
  			$('#comments').html(data);
  			//alert('Load was performed.');
		});
  		return false;
  	});
  
  } // end post comments...
  
  if($('textarea.jgrow').length>0){
  		$('textarea.jgrow').jGrow();
  }
  
  if($('#location').length>0){
  		jQuery("#location").jBreadCrumb();
  }
  
  if($('.editme').length>0){
    
    $('.editme').editable('/includes/save_editable.php',{
	    	submit : 'OK'
    	});
    
    
  }
  
  if($('textarea.tinymce').length>0)
  {
  		$('textarea.tinymce').tinymce({
			script_url : 'js/tiny_mce/tiny_mce.js',
	        theme : "advanced",
	        plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,contextmenu,paste,directionality,noneditable,visualchars,nonbreaking,xhtmlxtras",
      		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect,styleprops",
      		theme_advanced_buttons2 : "pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,forecolor,backcolor,attribs,del,ins",
      		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,|,sub,sup,|,emotions,media,cite,abbr,acronym",
      		theme_advanced_toolbar_location : "top",
      		theme_advanced_toolbar_align : "left",
      		theme_advanced_statusbar_location : "bottom",
      		theme_advanced_resizing : true
		});
   }


/*
if($('a[rel*=facebox]').length>0){
 	//$('a[rel*=facebox]').facebox();
	$("a[rel*=facebox]").click(function(){
		$("#facebox").show();
	});
 };
*/ 

 if($('a.poplight[href^=#]').length>0)
 {
  $('a.poplight[href^=#]').click(function() {
    var popID = $(this).attr('rel'); //Get Popup Name
    var popURL = $(this).attr('href'); //Get Popup href to define size

    //Pull Query & Variables from href URL
    var query= popURL.split('?');

    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value
    var catID = (dim[1].split('=')[1]);
 
 
  	 $("#root_id").val(catID);
 

    //$('#fileInput').uploadifySettings('scriptData', {'root_id' : '753' });

    var catName = (dim[2].split('=')[1]);
    $("#ulName").html(catName);
    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="/gfx/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;
  });

   //Close Popups and Fade Layer
   $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
    $('#fade , .popup_block').fadeOut(function() {
        $('#fade, a.close').remove();  //fade them both out
    });
    return false;
  });

}



 $(document).keydown(function(e) {
  		
      switch(e.keyCode) { 
         // User pressed "left" arrow
         case 37:
            reloadPhoto('#nav_previous');
         break;
         // User pressed "right" arrow
         case 39:
            reloadPhoto('#nav_next');
         break;
      }
   });

 $(".nav_gumb").click(function(){
	reloadPhoto(this);
 	return false;
 });
}); // end document.ready


function reloadMe(id){
	$.getJSON('/photo.php?id='+id+'&api=ajax', function(data) {
		window.location.hash = "/photo/"+data.photo_id;
		document.title = data.photo_title;
	    $("#current_photo").html(data.current_photo);
		$("#main_photo").attr("src",data.photo_url);

		$("#nav_first").attr("href",data.nav_first);
		$("#nav_previous").attr("href",data.nav_previous);
		$("#nav_next").attr("href",data.nav_next);
		$("#nav_last").attr("href",data.nav_last);

		$("#photo_title").html(data.photo_name);
		$("#photo_title2").html(data.photo_name);
		$("h3#photo_title2 div").html(data.photo_name);
		$("#photo_resolution").html(data.photo_resolution);
		$("#photo_author").html(data.photo_author);
		$("#photo_format").html(data.photo_format);
		$("#photo_category").html(data.photo_category);
		$("#photo_camera").html(data.photo_camera);
		$("#photo_camera_model").html(data.photo_camera_model);
		$("#photo_date").html(data.photo_taken_date);
		$("#photo_time").html(data.photo_taken_time);
		$("#photo_submited").html(data.photo_submited);
		$("#photo_views").html(data.photo_views);
		$("#photo_filesize").html(data.photo_filesize);
		$("#photo_num_rates").html(data.photo_number_rates);
		$("#photo_rating").html(data.photo_rating);

		$("div.editme").attr("id","photo_title___"+data.photo_id);
		$("div.editme").html(data.photo_name);

   });
}

function reloadPhoto(obj,m_url){
 	var m_url;
 	m_url = $(obj).attr("href");
	$.getJSON(m_url+'&api=ajax', function(data) {
		window.location.hash = "/photo/"+data.photo_id;
		document.title = data.photo_title;
	    $("#current_photo").html(data.current_photo);
		$("#main_photo").attr("src",data.photo_url);

		$("#nav_first").attr("href",data.nav_first);
		$("#nav_previous").attr("href",data.nav_previous);
		$("#nav_next").attr("href",data.nav_next);
		$("#nav_last").attr("href",data.nav_last);

		$("#photo_title").html(data.photo_name);
		$("#photo_title2").html(data.photo_name);
		$("#photo_description").html(data.photo_description);
		$("#photo_resolution").html(data.photo_resolution);
		$("#photo_author").html(data.photo_author);
		$("#photo_format").html(data.photo_format);
		$("#photo_category").html(data.photo_category);
		$("#photo_camera").html(data.photo_camera);
		$("#photo_camera_model").html(data.photo_camera_model);
		$("#photo_date").html(data.photo_taken_date);
		$("#photo_time").html(data.photo_taken_time);
		$("#photo_submited").html(data.photo_submited);
		$("#photo_views").html(data.photo_views);
		$("#photo_filesize").html(data.photo_filesize);
		$("#photo_num_rates").html(data.photo_number_rates);
		$("#photo_rating").html(data.photo_rating);

		$("div.editme").attr("id","photo_title___"+data.photo_id);
		$("div.editme").html(data.photo_name);


//  echo '<h3 id="photo_title2"><div id="photo_title___'.$ar[id].'" class="editme">'.$ar[name].'</div></h3>'."\n";


   });

}

function Confirm(link,text) 
{
 if(confirm(text))
    window.location=link;
}

function checkAll(field)
{
  for(i = 0; i < field.elements.length; i++)
     field[i].checked = true ;
}

function uncheckAll(field)
{
 for(i = 0; i < field.elements.length; i++)
    field[i].checked = false ;
}

function hidePreview()
{
 $("#preview_box").html("");
}

function updatePreview(id,event)
{
 $("#preview_box").load("includes/updatePreview.php?id="+id);
}

function checkDomain()
{
 var urljek = document.mysignup.nlogin.value;
 $("#domainBox").load("includes/checkDomain.php?url="+urljek);
}

function uploadBox(id)
{
 //$('#uploadBox').css("top",mouseY+"px");
 //$('#uploadBox').css("left",mouseX+"px");
 $('#uploadBox').load('/includes/upload_flash_form.php?id='+id).show();
 //$('#uploadBox').animate({opacity: 'show'}, "slow", "easein");
 //$('#uploadBox').draggable();
}

function closeUploadForm()
{
 $('#uploadBox').hide();
}