$(function(){
	
	var ajaxLoader = '<div style="text-align:center;"><img src="/img/ajax-loader.gif" /></div>';
	
	$('img, div, li').css('behavior','url(css/iepngfix.htc)');
	
	////////////////////////////////////////////////////
	//               CATEGORIES
	////////////////////////////////////////////////////


	$('.deleteCat').click(function(event){
		return confirm('Are you sure you want to delete this category?');
	});

	
	$('#lnkAddCat, #lnkAddSubcat').click(function(event){
		$('#saveCat').html(ajaxLoader).load('/admin/catForm.php');
		showSaveBox();
	});
	
	$('.editCat').click(function(event){
		
		var id = $(this).attr('title');
		$('#saveCat').html(ajaxLoader).load('/admin/catForm.php?id='+id);
		showSaveBox();
	});
	
	
	////////////////////////////////////////////////////
	//               LINKS
	////////////////////////////////////////////////////
	$('.deleteLink').click(function(event){
		return confirm('Are you sure you want to delete this link?');
	});
	
	$('#lnkAddLink').click(function(event){
		$('#content-right').load('/admin/linkForm.php');
		showSaveBox();
	});
	
	$('.editLink').click(function(event){
		
		var id = $(this).attr('title');
		$('#content-right').load('/admin/linkForm.php?id='+id);
		showSaveBox();
	});
	
	
	////////////////////////////////////////////////////
	//               LINKS
	////////////////////////////////////////////////////
	$('.deleteTestimonial').click(function(event){
		return confirm('Are you sure you want to delete this testimonial?');
	});
	
	$('#lnkAddTestimonial').click(function(event){
		$('#content-right').load('/admin/testimonialForm.php');
		showSaveBox();
	});
	
	$('.editTestimonial').click(function(event){
		
		var id = $(this).attr('title');
		$('#content-right').load('/admin/testimonialForm.php?id='+id);
		showSaveBox();
	});
	
	
	////////////////////////////////////////////////////
	//               COUNTRIES
	////////////////////////////////////////////////////
	$('.deleteCountry').click(function(event){
		return confirm('Are you sure you want to delete this country?');
	});
	
	$('#lnkAddCountry').click(function(event){
		$('#content-right').load('/admin/countryForm.php');
		showSaveBox();
	});
	
	$('.editCountry').click(function(event){
		
		var id = $(this).attr('title');
		$('#content-right').load('/admin/countryForm.php?id='+id);
		showSaveBox();
	});
	
	
	////////////////////////////////////////////////////
	//               BLOGS
	////////////////////////////////////////////////////
	$('.deleteBlog').click(function(event){
		return confirm('Are you sure you want to delete this blog entry?');
	});
	
	$('#lnkAddBlog').click(function(event){
		$('#content-right').load('/admin/blogForm.php');
		showSaveBox();
	});
	
	$('.editBlog').click(function(event){
		
		var id = $(this).attr('title');
		$('#content-right').load('/admin/blogForm.php?id='+id);
		showSaveBox();
	});
	
	$('.show_comments_admin').click(function(event){
		
		var id = $(this).attr('title');
		$('#content-right').load('/admin/loadComments.php?id='+id);

		return false;
	});
	
	$('.show_comments').click(function(event){
		
		var id = $(this).attr('title');
		$('#blog_comments_'+id).load('/loadComments.php?id='+id);
		return false;
	});
	
	$('a').click(function(event){
		$(this).blur();
	});
	
	
	//////////////////////////////////////////////
	//				IMAGES
	//////////////////////////////////////////////
	
	$('.show_cat_images').click(function(event){
		var id = $(this).attr('title');
		$('#content-right').html(ajaxLoader).load('/admin/cat_images.php?id='+id,function(){
		});
		
	});
	
	
	$('.image-box').click(function(event){
		var id = $(this).attr('id').replace('img','');
		var fadeSpeed = 250;
		
		if($.browser.msie && parseFloat($.browser.version) < 7){
			$('#image_info').load('/image_info.php?id='+id);
		}else{
			$('#image_info').animate({opacity:0.0},fadeSpeed,function(){
				$(this).load('/image_info.php?id='+id,function(){
					$(this).animate({opacity:1.0},fadeSpeed);
				});
			});
		}
		
		$('.bigimg1_wrap').animate({opacity:0},fadeSpeed,function(){
			$('#content-right-gallery').load('/image.php?id='+id,function(){
				$('.bigimg1_wrap').css('opacity','0');
				
				if($.browser.mozilla && parseFloat($.browser.version) < 1.9){
					intHeight = parseInt($('.bigimg1_wrap img').css('height').replace('px','')) - 1;
					$('.bigimg1_wrap img').css('height',intHeight+'px');
				}
				
				$('.bigimg1_wrap').animate({opacity:1},fadeSpeed,function(){
					if($.browser.msie){
						$('.bigimg1_wrap').css('filter','');
					}
				});
			});
		});
	});
	
	$('input[@type=checkbox]').click(function(){
		$(this).blur();
	});
	
	
	$('li.nopics a').click(function(){
		return false;
	});
	$('li.nopics a').css('cursor','default')
	
	$('table tr:even td').not('table.adminTable tr td').css('background','#dadad9');
	
	$('a.cq').click(function(){
		var id = $(this).attr('id').replace('lnk','');
		$('#item'+id+' div').css('display','none');
		$('#item'+id+' form').css('display','block');
	});
	
	
	$('#search_text').click(function(){
		if($(this).val()=='search'){
			$(this).val('');
		}
	});
	
	//$('#twinkle div').css('opacity',0);
	
	
	
	$('input:contains("delete")').each(function()
	{
		$(this).click(function()
		{
			return confirm('Are you sure you want to delete this?');
		});
	});
	
	
	
	$('a.zoom').fancyZoom().click(function(){
		return false;
	});

	//$('.image_title').css('opacity',0.75);
	
	$('.deleteOrder').click(function(){
		return confirm('Are you sure you want to delete this order?');
	});
	
	$('a.image-box-shop').mouseover(function(){
		$('.mag_glass',$(this).parent()).css('display','block');
	}).mouseout(function(){
		$('.mag_glass',$(this).parent()).css('display','none');
	});
	
	/*$('#table_buy_prints .buy_print').attr('style','padding-bottom:20px;`');*/
	
	$('img').each(function(){
		if(($(this).attr('title')=='' || $(this).attr('title')==undefined) && $(this).attr('alt')!=''){
			$(this).attr('title',$(this).attr('alt'));
		}
	});
	
	
	
});

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

var arrLoadedImages = new Array();

function twinkle(){
	var strArray = '';
	var imageFadeSpeed = 2000;
	
	
	for(i=0;i<arrImagesShowing.length; i++){
		strArray = strArray+' '+arrImagesShowing[i];
	}
	
	for(i=1;i<=9;i++){
		if($('#twinkle_'+i).css('opacity') > 0.1){
			var found = false;
			for(j=0;j<arrImagesShowing.length; j++){
				if(arrImagesShowing[j] == i){
					found = true;
				}
			}
			if(found==false){
				$('#twinkle_'+i).animate({opacity: 0},imageFadeSpeed);
			}
		}
	}
	
	if(arrImagesShowing[0] != ''){
		$('#twinkle_'+arrImagesShowing[0]).animate({opacity: 0},imageFadeSpeed,function(){
			arrImagesShowing.shift();
		});
	}
	
	var rand_no = getRandomNumber(1,9);
	while($('#twinkle_'+rand_no).css('opacity')!=0){
		rand_no = getRandomNumber(1,9);
	}
	arrImagesShowing.push(rand_no);
	
	//$('#twinkle_'+rand_no+' img').css('opacity','0');
	$('#twinkle_'+rand_no+' img').attr('src','/'+arrLoadedImages[currentTwinkleImage]).height(65);
	
	var file = arrLoadedImages[currentTwinkleImage].substring(arrLoadedImages[currentTwinkleImage].indexOf('images/')+7);
	var id = file.replace('_t.jpg','');
	
	$('#twinkle_'+rand_no+' a').attr('href','/find-image/'+id+'/');
	/*
	if($.browser.msie && $.browser.version < 7){
		
	}else{
		//$('#twinkle_'+rand_no+' img').css('opacity','1').animate({opacity:1},200,);
		
		var imageH = $('#twinkle_'+rand_no+' img').height();
		var divH = $('#twinkle_'+rand_no).height();
		var margin = (divH - imageH) / 2;
		//alert(margin+' = +('+divH+' - '+imageH+') / 2');
		$('#twinkle_'+rand_no+' img').css('margin-top',margin+'px');
	}
	*/
	$('#twinkle_'+rand_no+' img').css('margin-top','1px');
	
	$('#twinkle_'+rand_no).css({
		opacity:0
	}).animate({opacity: 1},imageFadeSpeed);
	
	currentTwinkleImage++;
	if(currentTwinkleImage == arrLoadedImages.length){
		currentTwinkleImage = 0;
	}
}

var currentTwinkleImage = 0;
var arrImagesShowing = ['1','2','3'];

function goTwinkle(){
	
	if(arrImages[0] != ''){
	
		var i;
	
		for(i=0;i<9;i++){
			if(arrImages[i] != undefined){
				preloadImg(arrImages[i]);
			}
		}
		
		setInterval(function(){
			if(i < arrImages.length){
				preloadImg(arrImages[i]);
				i = i + 1;
			}
			twinkle();
		},2000);
	}
}

function preloadImg(strImageLoc){
	var img = $('<img>');
	img.attr('src',strImageLoc);
	//console.log('preloading image : '+strImageLoc);
	if (img.complete == undefined) {
		img.attr('onload', function(){ 
			//console.log('preloaded image : '+strImageLoc);
			arrLoadedImages.push(strImageLoc);
		});
	}else{
		//console.log('already preloaded image : '+strImageLoc);
		arrLoadedImages.push(strImageLoc);
	}
}

function getRandomNumber(min,max){
	return Math.round((max)*Math.random()) + min;
}

function checkF1(){
	if($('#category_f1').attr('checked')){
		$('#cat_form_f1').css('display','block');
	}else{
		$('#cat_form_f1').css('display','');
	}
}

function checkUserPass(){
	if(!$('#category_secure').attr('checked')){
		$('#category_username').attr('disabled','disabled');
		$('#category_password').attr('disabled','disabled');
	}else{
		$('#category_username').attr('disabled','');
		$('#category_password').attr('disabled','');
	}
}



function showSaveBox(){
	$('.saveForm').css({'display':'block','opacity':'0'});
	$('.saveForm').animate({opacity:1.0},250,function(){
		checkUserPass();
		checkF1();
	});

}

function RemoveLoading(){
	$('#loading').remove();
}
function ShowLoading(){
	$('#loading').css('display','block');
}
function HideLoading(){
	$('#loading').animate({opacity:0},500,function(){
		$(this).remove();
	});
}
