﻿                 function clear_form()
                 {
                    jQuery("#contact_popup .content .errors").html('');
                    jQuery("#contact_popup .content .name_errors").html('');
                    jQuery("#contact_popup .content .email_errors").html('');
                    jQuery("#contact_popup .content .message_errors").html('');
                    jQuery("#contact_popup .content #id_sender_name").attr({value:''});
                    jQuery("#contact_popup .content #id_email").attr({value:''});
                    jQuery("#contact_popup .content #id_message").attr({value:''});
                    
                 
                 }

   jQuery(document).ready(function(){
                    
                jQuery(".example").click(function(){
                    
                    var largePath = jQuery(this).attr("image");
                    var largeTitle = jQuery(this).attr("title");
                    jQuery(".image_container .image").attr({ src: largePath, title: largeTitle });
                    jQuery(".example.current").prev().hide();
                    jQuery(".example.current").attr({'href': '', 'class':"example"});
                    jQuery(this).attr({ 'class':'example current'});
                    jQuery(this).removeAttr('href');
                    jQuery(this).prev().show();
                    return false;
                });
                
				jQuery('#skills a').click(function() {
					jQuery(this).toggleClass('simple');
					jQuery('#skills_popup').toggle();
					neighbour = jQuery('#employ_popup');
					if(neighbour.hasClass('popup_over'))
					{
						neighbour.removeClass('popup_over');
					}
					jQuery('#skills_popup').addClass('popup_over');
					
				
				});
				
				jQuery('#employees .show_employees').click(function() {
					jQuery(this).toggleClass('simple');
					jQuery('#employ_popup').toggle();
					
					
					neighbour = jQuery('#skills_popup');
					if(neighbour.hasClass('popup_over'))
					{
						neighbour.removeClass('popup_over');
					}
					jQuery('#employ_popup').addClass('popup_over');
					
				
				});
                
                jQuery('html').ajaxSend(function(event, xhr, settings) {
                    function getCookie(name) {
                        var cookieValue = null;
                        if (document.cookie && document.cookie != '') {
                            var cookies = document.cookie.split(';');
                            for (var i = 0; i < cookies.length; i++) {
                            var cookie = jQuery.trim(cookies[i]);
                            // Does this cookie string begin with the name we want?
                                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                                break;
                                }
                            }   
                        }
                        return cookieValue;
                    }
                    if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
                    // Only send the token to relative URLs i.e. locally.
                    xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
                        }
                    });
                
                function collapse()
                {
                        var products = jQuery("#older_products");
                        products.removeAttr('opened');
                        jQuery('#show_others').html(jQuery('#show_others').attr('data'));
                        jQuery('#show_others').attr({ title: jQuery('#show_others').attr('data')});
                        jQuery('#show_others').next().html('&darr;');
                
                }
                
                function open()
                {
                        var products = jQuery("#older_products");
                        products.attr({opened: 'opened'});
                        jQuery('#show_others').html('Спрятать');
                        jQuery('#show_others').next().html('&uarr;');
                        jQuery('#show_others').attr({ title: 'Скрыть более давние проекты'});
                
                }
                
                 jQuery("#show_others").click(function(){
                 
                    var products = jQuery("#older_products");
                    if(products.attr('opened'))
                    {
                        products.animate({height: "toggle"}, 800,collapse());                    
                    }
                    else {                    
                        products.animate({height: "toggle"}, 800,open());                        
                    }
            
                });
        

                 
                 function add_html(item, element){
                     jQuery("#contact_popup .content ."+element).append(item+ "<br/>");
                    
                 }
                 
                 jQuery('#send_contact').click(function(){
                   jQuery.get(
                        "/contacts/send/",
                        {
                            sender_name: jQuery("#id_sender_name").attr('value'),
                            message: jQuery("#id_message").attr('value'),
                            email: jQuery("#id_email").attr('value'),
                    },
                    function(data) {
                        data = JSON.parse(data);
                        if(data.result == 'success'){
                            jQuery("#contact_popup .content .result").html('Ваше сообщение отправлено');
                            clear_form();
                            Login.clearUserNameAndPassword();

                        }
                        if (data.result == 'error')
                        {
                            jQuery("#contact_popup .content .result").html('');
                            jQuery("#contact_popup .content .errors").html('');
                            if(data.errors){
                            data.errors.each(function(item){
                                jQuery("#contact_popup .content .errors").append(item+ "<br/>");
                            });}                            
                            jQuery("#contact_popup .content .name_errors").html('');
                            if(data.name_errors){
                            data.name_errors.each(function(item){
                                jQuery("#contact_popup .content .name_errors").append(item+ "<br/>");
                            });}                                                              
                            jQuery("#contact_popup .content .email_errors").html('');
                            if (data.email_errors){
                            data.email_errors.each(function(item){
                                jQuery("#contact_popup .content .email_errors").append(item+ "<br/>");
                            });}                                                                     
                            jQuery("#contact_popup .content .message_errors").html('');
                            if(data.message_errors){
                            data.message_errors.each(function(item){
                                jQuery("#contact_popup .content .message_errors").append(item+ "<br/>");
                            });}                                    
                        
                        }
                        
                    }
                    );
                        
                    
                 });
});

