function onAfter() {
    $('#imgtext').html(this.rel);
}

$(document).ready(function()
{
initSlideShow();
//initGalery();
	
});


function reloadCaptcha(path)
{
	$("#captImage").attr('src','/kcaptcha/index.php?chk='+Math.random(150000));
}


function loadOtherInfo(imgId)
{
	var _id = imgId.split("_");
	var DbId =_id[_id.length-1];
	
	//alert(DbId);
	
	/*
	photo_location-
	
	photo_byButton
	photo_photographers-
	*/
	$("#photo_location").html($("#photo_location-"+DbId).html());
	$("#photo_byButton").html($("#photo_byButton-"+DbId).html());
	$("#photo_photographers").html($("#photo_photographers-"+DbId).html());
	
	var _id = $("#persent_link_"+DbId);
	_href=_id.attr('href');
	$("#myPresent").attr('href',_href);
}

function initGalery()
{
	var _thumbs = $("#thumbs");
				if(_thumbs.length > 0){
				var onMouseOutOpacity = 0.9;
				$('#thumbs ul.thumbs li, div.navigation a.pageLink').opacityrollover({
					mouseOutOpacity:   onMouseOutOpacity,
					mouseOverOpacity:  1.0,
					fadeSpeed:         'slow',
					exemptionSelector: '.selected'
				});
				
				// Initialize Advanced Galleriffic Gallery
				var gallery = $('#thumbs').galleriffic({
					delay:                     2500,
					numThumbs:                 7,
					preloadAhead:              7,
					enableTopPager:            false,
					enableBottomPager:         false,
					imageContainerSel:         '#slideshow',
					controlsContainerSel:      '#controls',
					captionContainerSel:       '#caption',
					loadingContainerSel:       '#loading',
					renderSSControls:          true,
					renderNavControls:         true,
					playLinkText:              '',
					pauseLinkText:             '',
					prevLinkText:              '',
					nextLinkText:              '',
					nextPageLinkText:          '',
					prevPageLinkText:          '',
					enableHistory:             true,
					autoStart:                 false,
					syncTransitions:           true,
					defaultTransitionDuration: 900,
					onSlideChange:             function(prevIndex, nextIndex) {
						
						// 'this' refers to the gallery, which is an extension of $('#thumbs')
						this.find('ul.thumbs').children()
							.eq(prevIndex).fadeTo('slow', onMouseOutOpacity).end()
							.eq(nextIndex).fadeTo('slow', 1.0);

						// Update the photo index display
						this.$captionContainer.find('div.photo-index')
							.html('Photo '+ (nextIndex+1) +' of '+ this.data.length);
							
					},
					onPageTransitionOut:       function(callback) {
						
						this.fadeTo('slow', 0.0, callback);
					},
					onPageTransitionIn:        function() {
						var prevPageLink = this.find('a.galprev').css('visibility', 'hidden');
						var nextPageLink = this.find('a.galnext').css('visibility', 'hidden');
						
						// Show appropriate next / prev page links
						if (this.displayedPage > 0)
							prevPageLink.css('visibility', 'visible');

						var lastPage = this.getNumPages() - 1;
						if (this.displayedPage < lastPage)
							nextPageLink.css('visibility', 'visible');

						this.fadeTo('slow', 1.0);
					}
				});

				/**************** Event handlers for custom next / prev page links **********************/

				gallery.find('a.galprev').click(function(e) {

					gallery.previousPage();
					e.preventDefault();
				});

				gallery.find('a.galnext').click(function(e) {
					gallery.nextPage();
					e.preventDefault();
				});

				/****************************************************************************************/

				/**** Functions to support integration of galleriffic with the jquery.history plugin ****/

				// PageLoad function
				// This function is called when:
				// 1. after calling $.historyInit();
				// 2. after calling $.historyLoad();
				// 3. after pushing "Go Back" button of a browser
				function pageload(hash) {
					
					// hash doesn't contain the first # character.
					if(hash) {
						$.galleriffic.gotoImage(hash);
					} else {
						gallery.gotoIndex(0);
					}
					
				}

				// Initialize history plugin.
				// The callback is called at once by present location.hash. 
				$.historyInit(pageload,window.location);
				
				getFirst();
				// set onlick event for buttons using the jQuery 1.3 live method
				$("a[rel='history']").live('click', function(e) {
					if (e.button != 0) return true;
					
					if($(this).hasClass('advance-link'))
					{
						
						var _x = $(this).children();
					
						var _img_src  =_x[0].src;
						this.href =_img_src;
						
						//alert(_img_src);
						_href= _img_src.replace('view.php?img=big/','img/products/original/');
						//alert(_href);
						_href= _href.replace('/big/t_','/original/');
						_href  = _href.replace('.png','.jpeg');
						//this.href=_href;
						
						
						
						$(this).colorbox({href:_href,open:true});
						return false;
					}else{

					var hash = this.href;
					
					hash = hash.replace(/^.*#/, '');

					// moves to a new page. 
					// pageload is called at once. 
					// hash don't contain "#", "?"
					$.historyLoad(hash);


					return false;
					}
				});
				
				
				}
				
				//$(".activeThumb").each( function(){ window.location=this, loadOtherInfo(this.id)});
}
function getFirst()
{
	x = $(".activeThumb");
	_y = x[0];

	$(_y).click()
	
	
}
function initSlideShow()
{
		$('#slideshow img:first').fadeIn(1000, function() {
	    $('#slideshow').cycle({
		speed:  400, 
        timeout:5000,
        pager:  '#pagernav',
		after:   onAfter
   		});
	});
}


function _loader(place)
{
	
	var _ajaxs_splesh = document.getElementById('ajaxs_splesh');
	
	if(_ajaxs_splesh)
	this.splesh = _ajaxs_splesh;
	
	var _img = new Image();
	_img.className = "splesh_loader";
	_img.src = "/images/19-1.gif";
	var _slpesh = document.createElement('div');
	_slpesh.id = "ajaxs_splesh"
	_slpesh.className = "spalsh";
	_slpesh.appendChild(_img);
	_pos = $(place).position();
	_w = $(place).outerWidth();
	_h = $(place).outerHeight();
	
	$(_slpesh).css({top:_pos.top,left:_pos.left,width:_w,height:_h}).appendTo(place);
	_it =Math.round(_h*0.1);
	_il =Math.round(_w*0.5);
	
	$(_img).css({top:_it,left:_il});
			
	this.splesh	=_slpesh;	
	
	
}

_loader.prototype.show = function()    // Define Method
{
    $(this.splesh).show();
}

_loader.prototype.hide = function()    // Define Method
{
	
	_alert = $(".spalsh");
	$(".spalsh").each ( function(e,i){$(i).remove()})
	
}

function loginAction(form,tpl,holder)
{
	
	_url = $(form).attr('action');
	_url =_url+"?external=1&itpl="+tpl;
	
	var loader = new _loader(holder);
	
	loader.show();
	_data = $(form).serialize();
	$.post(_url,_data,function(response){
							   loader.hide();
							   $(holder).replaceWith(response)
							   
							   });
	
	return false; 
	
}

function editUserInfo(_form)
{
	_data = $(_form).serialize();
	_url = $(_form).attr('action');
		var loader = new _loader(holder);
	
	loader.show();
	$.post(_url,_data,function(response){loader.hide(); $(_form).replaceWith(response)});
	return false;
}

function _initMyPager(mclassName)
{
	

	$(mclassName).click (
						 
						 function(){
	$.get(this,function(data){ $('#cccc').replaceWith(data);});
	
	return false;
	});
	
								 


}
var validateAndUpload = function(_this,message)
{
	try{
	_filname = _this.upload.value;
	var testExt = new RegExp(/.\.(jpe?g||gif||png)$/i);
	
	_valid = testExt.test(_filname);
	if(!_valid)
	{
		
		alert(message);
	}else
	{
		holder = document.getElementById('action-div');
		
		
		var loader = new _loader(holder);
	
		loader.show();
		
		_this.submit();
	}
	
	return _valid;
	}catch(e){ alert(e);}
}
var AvatarUploaded = function(url,data)
{
	
	holder = document.getElementById('action-div');
		
		
		var loader = new _loader(holder);
	
		loader.show();
	
	$.post(url,data,function(msg){$('#Profile_avatar').replaceWith(msg); loader.hide();});
}
var  uploadSucsess=function(path,data)
{
		
	
	$.post(path,data,function(msg){ 
							  
						  holder = document.getElementById('action-div');
		
		
		var loader = new _loader(holder);
 
	
		loader.hide();
							  $("#images_here").html(msg);
							  
							  
							  })
}

var cropperInit = function(place,image,LOADING_MSG,path)
{
	
	
	var _cropper = document.createElement('div');
	
	_cropper.innerHTML = LOADING_MSG;
	$(_cropper).dialog({modal:true, width:800,height:600, close:function(){$(this).remove()}});
	
	$.post(path,{'act':'croper-view',place:place,'image':image},function(_DATA_){$(_cropper).html(_DATA_)})
}
function savePhoto(form)
{
	_data = $(form).serialize();
	_action =$(form).attr('action');
				  holder = document.getElementById('action-div');
		
		
		var loader = new _loader(holder);
 
	
		loader.show();
	$.post(_action,_data,function(msg){
								  
								  $('#form_continer').replaceWith(msg); 
																	 
																	   loader.hide();});
	
	return false;
}

function comment(URL1,URL2)
{
	$(".bigphoto").each(function(index, element) {
		_id = document.getElementById(element.id)

		if(_id.style.display == 'block')
		{
			_idint = _id.id;
			_idint = _idint.replace("big","");

			_url = "/"+URL1+"/"+URL2+"/"+_idint+".html";

			window.location = _url;
			return;
		}        
    });

}

function sendCommentinfo(response)
{
	
var _url = "/photo";
	$.ajax({
	url:_url,
	type:'get',
	data:{commentID:response.commentID,href:response.href,mailto:1},
	success: function(data)
				{
				}
	});
	
	return false;
}
