var currenthash = window.location.hash;
var flaghash = true;
function loadLink() {
	if (window.location.hash.length > 2 && window.location.hash.length < 12) {
		//bio and contacts
		attrhash = window.location.hash.replace( /_.*/, "");
		switch (attrhash) {
			case "#biografy":
				jQuery.loadContentLink("#biografy","Биография","bio","bio","1");
				break;
			case "#contacts":
				jQuery.loadContentLink("#contacts","Контакты","contacts","contacts","1");
				break;
			default:
				break;
		}

	} else {
		if (window.location.hash.length > 11 && window.location.hash.length < 15) {
			//osnovnie razdeli and pages
			temphash =  window.location.hash.replace( /#.*_p/, "");
			attrhash = window.location.hash.substr(0,9);
			switch (attrhash) {
				case "#myevents":
					jQuery.loadContentLink(0,"События","event","eventlist", temphash);
					break;
				case "#my_music":
					jQuery.loadContentLink(0,"Музыка","music","musiclist",temphash);
					break;
				case "#my_teatr":
					jQuery.loadContentLink(0,"Театр","teatr","teatrlist",temphash);
					break;
				case "#my_films":
					jQuery.loadContentLink(0,"Кино","kino","kinolist",temphash);
					break;
				case "#myalbums":
					jQuery.loadContentLink(0,"Фотографии","foto","albumlist",temphash);
					// Load the classic theme Galleria
					Galleria.loadTheme('js/galleria.classic.js');
					break;
				case "#my_video":
					jQuery.loadContentLink(0,"Видео","video","videolist",temphash);
					break;
				case "#my_press":
					jQuery.loadContentLink(0,"Пресса","press","pressa",temphash);
					break;
				case "#myafisha":
					jQuery.loadContentLink(0,"Афиша","afisha","afishalist",temphash);
					break;
				default:
					break;
			}
		} else {
			//
			if (window.location.hash.length > 15) {
				//articles
				temphash =  window.location.hash.replace( /#.*_/, "");
				attrhash = window.location.hash.substr(0,9);
				switch (attrhash) {
					case "#myevents":
						jQuery.loadContentLink(0,"События","event","event", temphash);
						break;
					case "#my_music":
						jQuery.loadContentLink(0,"Музыка","music","music",temphash);
						break;
					case "#my_teatr":
						jQuery.loadContentLink(0,"Театр","teatr","teatr",temphash);
						break;
					case "#my_films":
						jQuery.loadContentLink(0,"Кино","kino","kino",temphash);
						break;
					case "#myalbums":
						jQuery.loadContentLink(0,"Фотографии","foto","fotolist",temphash);
						// Load the classic theme Galleria
						Galleria.loadTheme('js/galleria.classic.js');
						break;
					case "#my_video":
						jQuery.loadContentLink(0,"Видео","video","video",temphash);
						break;
					case "#my_press":
						jQuery.loadContentLink(0,"Пресса","press","pressa",temphash);
						break;
					case "#myafisha":
						jQuery.loadContentLink(0,"Афиша","afisha","afisha",temphash);
						break;
					default:
						break;
				}
			} else {
				if (!flaghash) {
					jQuery.closeContent();
				}
			}
		}
	} 
	}
function checkHash() {
        if (currenthash!=window.location.hash) {
                loadLink();
                currenthash=window.location.hash
        }
}
function loadLikes(urlhash, urla, hash) {
    if (urla=="afisha"||urla=="event"||urla=="bio"||urla.substr(0,9)=="musiclist"||urla=="teatr"||urla=="kino"||urla=="fotolist"||urla=="videolist"||urla=="pressa") 
    {
        //facebook
        $("#like_facebook").html("<iframe src='//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2Fpages%2F%25D0%259F%25D0%25BE%25D0%25BB%25D0%25B8%25D0%25BD%25D0%25B0-%25D0%259A%25D0%25BE%25D0%25B6%25D0%25B8%25D0%25BA%25D0%25BE%25D0%25B2%25D0%25B0%2F203953399652170&amp;send=false&amp;layout=button_count&amp;width=150&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:150px; height:21px;' allowTransparency='true'></iframe>");
        //vkontakte
        VK.Widgets.Like("vk_like", {type: "button"}, hash);
        //vkontakte share link
        //$("#like_vk2").html(VK.Share.button({url: 'http://www.polina-kozhikova.ry/'},{type: 'button', text: 'Мне нравится'}));
        //google
        $("#like_google").html("<g:plusone size='medium' href='http://www.polina-kozhikova.ru/"+urlhash+"'></g:plusone>");
        gapi.plusone.go();
    }
}
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
	var args_len = arguments.length;
	for (var i = args_len; i--;) {
	  var cacheImage = document.createElement('img');
	  cacheImage.src = arguments[i];
	  cache.push(cacheImage);
	}
  }
  $.loadContent = function(hash, urla, id) {
	flaghash = false;
	window.location.hash = hash; 
	currenthash = window.location.hash;
        $("#vk_like, #like_facebook, #like_google").html("");
	$("#bodywrap").html("<div class='loader'>Идет загрузка...</div>");
			$.post("/ajax/get"+urla+"/", { page: id},
				function(result){
					if (result.type == 'error') {
						$("#bodywrap").fadeOut(1, function () {
							$(this).html(result.event).fadeIn(1000);
							});
						return(false);
					}
					else {
						$("#bodywrap").fadeOut(1, function () {
							$(this).html(result.event).fadeIn(1000);
							if (urla == "fotolist") {
								$('#galleria').galleria();
							}
                                                        if (result.hash) {loadLikes(hash, urla, result.hash);}
						});
					}
				},
			"json");
  }
  $.loadContentLink = function(hash, allru, all, urla, id) {
	flaghash = false;
	if (hash !=0) {
	window.location.hash = hash;
	}
	currenthash = window.location.hash;
        $("#vk_like, #like_facebook, #like_google").html("");
	$(".pklink").html("").html("<a title='"+allru+"' class='all"+all+"'><img src='images/"+all+"_header.png' alt='"+allru+"' /></a>");
	$("#bodywrapper").addClass("body"+all+"").fadeIn(1000);
	$("#bodywrap").html("<div class='loader'>Идет загрузка...</div>").delay(1000).fadeIn(1);
			$.post("/ajax/get"+urla+"/", { page: id},
				function(result){
					if (result.type == 'error') {
						$("#bodywrap").fadeOut(1, function () {
							$(this).html(result.event).fadeIn(1000);
							});
						return(false);
					}
					else {
						$("#bodywrap").fadeOut(1, function () {
							$(this).html(result.event).fadeIn(1000);
							if (urla == "fotolist") {
								$('#galleria').galleria();
							}
                                                        if (result.hash) {loadLikes(hash, urla, result.hash);}
							});
						}
				},
			"json");
  }
  $.closeContent = function() {
		flaghash = true;
		window.location = window.location.href.replace( /#.*/, "");
		$("#bodywrap").fadeOut(100);
		$("#bodywrapper").fadeOut(100).removeClass("bodyevent").removeClass("bodybio").removeClass("bodymusic").removeClass("bodyteatr").removeClass("bodykino").removeClass("bodyfoto").removeClass("bodyvideo").removeClass("bodypress").removeClass("bodycontacts");
		$(".pklink").html("");
  }
})(jQuery)

$(document).ready(function(){
	//позиционируем меню по центру
	var pkmargint = 0;
	var pkmarginl = 0;
	if ($(window).height()>720) {pkmargint=$(window).height()/2-350;} else {pkmargint = 10;}
	$("#wrapper").css({marginTop: ''+pkmargint+'px'});
	$("#bodywrapper").css({marginTop: ''+pkmargint+'px'});
	if ($(window).width()>1010) {pkmarginl=$(window).width()/2-490;} else {pkmarginl = 5;}
	$("#bodywrapper").css({marginLeft: ''+pkmarginl+'px'});
	//скрипт меню-изображений
	$('.kwicks').kwicks({ max : 380, spacing : 1 });
	//preload images
	jQuery.preLoadImages("/images/fon_event.jpg", "/images/fon_bio.jpg", "/images/fon_music.jpg", "/images/fon_teatr.jpg", "/images/fon_kino.jpg", "/images/fon_foto.jpg", "/images/fon_video.jpg", "/images/fon_press.jpg", "/images/fon_contact.jpg");
	//proverka hash dlia next|prev
	setInterval("checkHash()", 50);
	//on-click menus items
	$("ul>li","#menuwrap").click(function () { 
		switch ($(this).attr("id")) {
			case "pevent":
				jQuery.loadContentLink("#myevents_p1","События","event","eventlist","1");
				break;
			case "pbio":
				jQuery.loadContentLink("#biografy","Биография","bio","bio","1");
				break;
			case "pmusic":
				jQuery.loadContentLink("#my_music_p1","Музыка","music","musiclist","1");
				break;
			case "pteatr":
				jQuery.loadContentLink("#my_teatr_p1","Театр","teatr","teatrlist","1");
				break;
			case "pkino":
				jQuery.loadContentLink("#my_films_p1","Кино","kino","kinolist","1");
				break;
			case "pfoto":
				jQuery.loadContentLink("#myalbums_p1","Фотографии","foto","albumlist","1");
				// Load the classic theme Galleria
				Galleria.loadTheme('js/galleria.classic.js');
				break;
			case "pvideo":
				jQuery.loadContentLink("#my_video_p0","Видео","video","videolist","0");
				break;
			case "ppress":
				jQuery.loadContentLink("#my_press_p1","Пресса","press","pressa","1");
				break;
			default:
				break;
		}
	});	
	//on-click afisha
	$("#pafisha").click(function () { 
		jQuery.loadContentLink("#myafisha_p1","Афиша","afisha","afishalist","1");
	});
	//on-click contacts
	$("#pkcontacts").click(function () { 
		jQuery.loadContentLink("#contacts","Контакты","contacts","contacts","1");
	});
	//on-click send in contacts
	$("#send").live('click', function () { 
		$.post("/ajax/sendcontacts/", {name: $("#sendname").val(), mail: $("#sendemail").val(), mes:$("#sendmessage").val()},
			function(result){
				if (result.type == 'error') {
					$("#mailerror").html(result.event);
					return(false);
				}
				else {
					$("#mailerror").html(result.event);
					$("#sendname").val('');
					$("#sendemail").val('');
					$("#sendmessage").val('');
					}
			},
		"json");
	});	
	//zapros spiska pri klike po all
	$("a",".pklink").live('click',function () { 
		switch ($(this).attr("class")) {
			case "allevent":
				jQuery.loadContent("#myevents_p1","eventlist","1");
				break;
			case "allbio":
				jQuery.loadContent("#biografy","bio","1");
				break;
			case "allmusic":
				jQuery.loadContent("#my_music_p1","musiclist","1");
				break;
			case "allteatr":
				jQuery.loadContent("#my_teatr_p1","teatrlist","1");
				break;
			case "allkino":
				jQuery.loadContent("#my_films_p1","kinolist","1");
				break;
			case "allfoto":
				jQuery.loadContent("#myalbums_p1","albumlist","1");
				// Load the classic theme Galleria
				Galleria.loadTheme('js/galleria.classic.js');
				break;
			case "allvideo":
				jQuery.loadContent("#my_video_p0","videolist","1");
				break;
			case "allpress":
				jQuery.loadContent("#my_press_p1","pressa","1");
				break;
			case "allcontacts":
				jQuery.loadContent("#contacts","contacts","1");
				break;
			case "allafisha":
				jQuery.loadContent("#myafisha_p1","afishalist","1");
				break;
			default:
				break;
		}
	});
	//zapros pagination
	$("a","#eventspagination").live('click', function () { 
		switch ($(this).attr("class")) {
			case "eventspage":
				jQuery.loadContent("#myevents_p"+$(this).attr("id"),"eventlist",$(this).attr("id"));
				break;
			case "musicpage":
				jQuery.loadContent("#my_music_p"+$(this).attr("id"),"musiclist",$(this).attr("id"));
				break;
			case "teatrpage":
				jQuery.loadContent("#my_teatr_p"+$(this).attr("id"),"teatrlist",$(this).attr("id"));
				break;
			case "kinopage":
				jQuery.loadContent("#my_films_p"+$(this).attr("id"),"kinolist",$(this).attr("id"));
				break;
			case "albumpage":
				jQuery.loadContent("#myalbums_p"+$(this).attr("id"),"albumlist",$(this).attr("id"));
				// Load the classic theme Galleria
				Galleria.loadTheme('js/galleria.classic.js');
				break;
			case "videopage":
				jQuery.loadContent("#my_video_p"+$(this).attr("id"),"videolist",$(this).attr("id"));
				break;
			case "pressapage":
				jQuery.loadContent("#my_press_p"+$(this).attr("id"),"pressa",$(this).attr("id"));
				break;
			case "afishapage":
				jQuery.loadContent("#myafisha_p"+$(this).attr("id"),"afishalist",$(this).attr("id"));
				break;
			default:
				break;
		}
	});	
	//zapros article event
	$(".eventid").live('click', function () { 
	jQuery.loadContent("#myevents_article_"+$(this).attr("id"),"event",$(this).attr("id"));
	});	
	//zapros events return	
	$(".returnevents").live('click', function () { 
		jQuery.loadContent("#myevents_p"+$(this).attr("id"),"eventlist",$(this).attr("id"));
	});	
	//zapros article teatr
	$(".teatrid").live('click', function () { 
	jQuery.loadContent("#my_teatr_article_"+$(this).attr("id"),"teatr",$(this).attr("id"));
	});	
	//zapros teatr return	
	$(".returnteatr").live('click', function () { 
		jQuery.loadContent("#my_teatr_p"+$(this).attr("id"),"teatrlist",$(this).attr("id"));
	});	
	//zapros article kino
	$(".kinoid").live('click', function () { 
	jQuery.loadContent("#my_films_article_"+$(this).attr("id"),"kino",$(this).attr("id"));
	});	
	//zapros kino return	
	$(".returnkino").live('click', function () { 
		jQuery.loadContent("#my_films_p"+$(this).attr("id"),"kinolist",$(this).attr("id"));
	});	
	//zapros foto pri klike po albomy
	$(".albumid").live('click', function () { 
		jQuery.loadContent("#myalbums_article_"+$(this).attr("id"),"fotolist",$(this).attr("id"));
	});	
	//zapros afisha event
	$(".afishaid").live('click', function () { 
	jQuery.loadContent("#myafisha_article_"+$(this).attr("id"),"afisha",$(this).attr("id"));
	});	
	//zapros afisha return	
	$(".returnafisha").live('click', function () { 
		jQuery.loadContent("#myafisha_p"+$(this).attr("id"),"afishalist",$(this).attr("id"));
	});		
	//on-click close
	$("#pkclose").click(function () { 
		jQuery.closeContent();
	});
	//generciya stranici pri zahode po pryamomy linky
	loadLink();
	
});

//resize
$(window).resize(function(){
	//позиционируем меню по центру
	var pkmargint = 0;
	var pkmarginl = 0;
	if ($(window).height()>720) {pkmargint=$(window).height()/2-350;} else {pkmargint = 10;}
	$("#wrapper").css({marginTop: ''+pkmargint+'px'});
	$("#bodywrapper").css({marginTop: ''+pkmargint+'px'});
	if ($(window).width()>1010) {pkmarginl=$(window).width()/2-490;} else {pkmarginl = 5;}
	$("#bodywrapper").css({marginLeft: ''+pkmarginl+'px'});
});
 
