﻿
$(document).ready(function(){

	oMain.photos.init()

});


oMain.photos = {
	i : 0,
	photoArr : new Array(),
	slide_show : false,
	title : '',
	type_id : 2,
	obj_id : 0,
	add_photo : 0,
	init : function(){
		$("#login").bind("logined", oMain.photos.initUpload);
		$("#show_photos a").click(function(event){
			event.stopPropagation();
			if($("#photos").is(':visible')){
				_gaq.push(['_trackEvent','photos', 'hide', oMain.photos.title]);
				$("#photos").slideUp("fast")
				oMain.photos.slide_show = false;
				}
			else {
				_gaq.push(['_trackEvent','photos', 'show', oMain.photos.title]);
				$("#photos").slideDown("fast");
				}
			return false;
			});

		$("#slide_show a").click(function(event){
			oMain.photos.slide_show = true;
			event.stopPropagation();
			_gaq.push(['_trackEvent','photos', 'slideshow', oMain.photos.title]);
			if(!$("#photos").is(':visible')) $("#photos").slideDown("fast");
			$("#photos a").each(function(){
				oMain.photos.photoArr.push($(this).attr('href'))
				});
			setTimeout(oMain.photos.slideShow, 2000);
			return false;
			});

		$("#photos a").live('click',function(event){
			event.preventDefault();
			_gaq.push(['_trackEvent','photos', 'click', oMain.photos.title]);
			oMain.photos.slide_show = false;
			oMain.photos.show($(this).attr('href'));
			});

		$('<img id="i2" src="/img/spc.gif" alt="" />').appendTo("#main_photo")
				.hide();
		if($('.cont_header').hasClass('place')) oMain.photos.type_id = 1;
		oMain.photos.obj_id = $('.cont_header span').attr('class');
		oMain.photos.title = $('.cont_header span:first').text();
		},
	show : function(src){
		$("#main_photo").css({'position': 'relative'})
				.height($("#main_photo").height());
		$("#i1, #i2").css({'position':'absolute',
				  'top': 0,
				  'left': 0});
		if($("#i1").is(':visible')){
			var sId =  '#i2',
				hId = '#i1'
			}
		else {
			var sId =  '#i1',
				hId = '#i2'
			};
		$(sId).attr('src', src)
			.load(function(){
				$('#main_photo').animate({'height' : $(this).height()});
				$(hId).fadeOut();
				$(sId).fadeIn();
				});
		return false;
		},
	slideShow : function(){
		if(oMain.photos.slide_show){
			oMain.photos.i++;
			if(oMain.photos.i > oMain.photos.photoArr.length -1) oMain.photos.i = 0;
			oMain.photos.show(oMain.photos.photoArr[oMain.photos.i]);
			setTimeout(oMain.photos.slideShow, 3000)
			}
		},
	initUpload : function(){
		$("#rating").append('<p><br /></p><div id="add_photo"><a href="#">Добавить фотографию</a></div>');
		oMain.utils.IncludeJavaScript('/js/ajaxupload.3.5.js');
		oMain.photos.add_photo = new AjaxUpload($("#add_photo a"), {
			action: '/ajax/photos.html',
			name: 'photofile',
			autoSubmit: true,
			data: { mode: 'uploadfile' },
			responseType: 'text',
			onSubmit: function() {
				oMain.photos.add_photo.disable();
				$("#add_photo a").text('фотография обрабатывается...');
				oMain.photos.add_photo.setData({ mode: 'uploadfile', type_id: oMain.photos.type_id, obj_id: oMain.photos.obj_id });
			},
			onChange: function(file, extension, fullpath){
				if (!extension || !/^(jpg|jpeg)$/.test(extension)){
					alert('Фотография должна быть в формате JPEG.');
					return false;
					}
				if (!oMain.ctrl.logined){
					alert('Необходимо авторизироваться!');
					return false;
					}
			},
			onComplete: function(file, response){
				photo_id = response;
				$.getJSON("/ajax/photos.html", { mode: "compile", file_id: photo_id, atonce:1 }, function(jArr){
					if(jArr.length > 1){
						var img_cont = $('<div class="photo"></div>');
						var i = $('<img alt="" />').attr('src',jArr[0].fullpath);
						$('<a></a>').attr('href',jArr[1].fullpath)
								.append(i)
								.appendTo(img_cont);
						$(img_cont).appendTo("#photos");
						}
					if(!$("#photos").is(':visible')) $("#photos").slideDown("fast");
					$("#add_photo a").text('Добавить фотографию');
					oMain.photos.add_photo.enable();
					});
			}
		});
		}
}
