$(document).ready(function(){
	
	
	function createCookie(name,value,days) {
	    if (days) {
	        var date = new Date();
	        date.setTime(date.getTime()+(days*24*60*60*1000));
	        var expires = "; expires="+date.toGMTString();
	    }
	    else var expires = "";
	    document.cookie = name+"="+value+expires+"; path=/";
	}
	 
	function readCookie(name) {
	    var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0;i < ca.length;i++) {
	        var c = ca[i];
	        while (c.charAt(0)==' ') c = c.substring(1,c.length);
	        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    }
	    return null;
	}
	 
	function eraseCookie(name) {
	    createCookie(name,"",-1);
	}
	
	
	
	$('.more-d').click(function() {
		var i = $(this).attr('rel');
		$(this).hide();
		$('#full-'+i).fadeIn(500);
		$('.break').filter('[rel='+i+']').show();
	});
	
	$('.break').click(function() {
		var i = $(this).attr('rel');
		$(this).hide();
		$('#full-'+i).fadeOut(300, function() {
			$('.more-d').filter('[rel='+i+']').show();
		});
	});
	
	$('.tags a').click(function() {
		$('.tags a').removeClass('active');
		$(this).addClass('active');
	});
	
	var rcon = $('#rcon');
	var st = rcon.offset();
	$(window).scroll(function() {
		var pos = window.pageYOffset || document.documentElement.scrollTop;
		if( pos > st.top ) {
			rcon.css({position : 'fixed', top : 0});
		} else {
			rcon.css({position : 'relative', top : 'auto', zoom : 1});
		}
	});
	
	
	/*if(window.location.href != 'http://cosmo.very-good.ru/') {
		var params_window = "width=300, height=200, top=0, left=0, toolbar=0,location=0,directories=0,menubar=0,scrollbars=0,resizable=0,status=0,fullscreen=0";
		if (readCookie('player') != 'open') {
			window.open("/player.html", "Player", params_window);
			createCookie('player', 'open');
		}
	}*/
		


	$("#sub-mail").focus(function() {
		if ($(this).val() == "E-mail рассылка") {
			$("#sub-mail").val("");
		}
	});
	$("#sub-phone").focus(function() {
		if ($(this).val() == "SMS рассылка") {
			$("#sub-phone").val("+7");
		}
	});
	$("#sub-name").focus(function() {
		if ($(this).val() == "Имя") {
			$("#sub-name").val("");
		}
	});

	$("#sub-submit").click(function() {
		var form = $("#subscribe");
		
		$.ajax({
			type: 'post',
			dataType: "html",
			url: '/ajax/subscribe.php',
			data: form.serialize(),
			beforeSend: function(){
				
			},
			success: function(data){
				if(data == 'ok') {
					alert("Вы успешно подписаны.");
					$("#sub-mail").val("E-mail рассылка");
					$("#sub-phone").val("SMS рассылка");
					$("#sub-name").val("Имя");
				}
				else {
					alert(data);
				}
			},
			error: function(){
				alert("Ошибка отправки.");
			}
		});
		return false;
	});
	
	$("#feed-submit").click(function() {
		var form = $("#feedback");
		
		$.ajax({
			type: 'post',
			dataType: "html",
			url: '/ajax/feedback.php',
			data: form.serialize(),
			beforeSend: function(){
				
			},
			success: function(data){
				if(data == 'ok') {
					alert("Ваше письмо успешно отправлено.");
				} else {
					alert(data);
				}
			},
			error: function(){
				alert("Ошибка отправки.");
			}
		});
		return false;
	});
	
	$("#vacancy-submit").click(function() {
		var form = $("#vacancy-form");
		
		$.ajax({
			type: 'post',
			dataType: "html",
			url: '/ajax/vacancy.php',
			data: form.serialize(),
			beforeSend: function(){
				
			},
			success: function(data){
				if( $('input[name="foto"]').val() ) {
					$('#delete-image').click();
				}
				if(data == 'ok') {
					alert("Ваше письмо успешно отправлено.");
				} else {
					alert(data);
				}
			},
			error: function(){
				alert("Ошибка отправки.");
			}
		});
		return false;
	});
	
	$(".js-print").click(function() {
		window.print();
	});
});

function parseJSON( data ) {
		if ( typeof data !== "string" || !data ) {
			return null;
		}
		// Make sure leading/trailing whitespace is removed (IE can't handle it)
		data = data.replace(/^\s*|\s*$/gi, '' );
		
		// Make sure the incoming data is actual JSON
		// Logic borrowed from http://json.org/json2.js
		if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
			.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
			.replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
		
			// Try to use the native JSON parser first
			return (new Function("return " + data+";"))();

		} else {
			throw "Invalid JSON: " + data ;
		}
	}
	
