$(document).ready(function(){
    if (!$.cookie("xmas")){
    	$("#popupNY").fadeIn(350);
	$.cookie("xmas", "1", {path:"/", expires: 0});
    }
    else {
    	$("#popupNY").hide(350);
    }
    $("#popupNY").click(function(){
        $(this).fadeOut(250);
    }); // click
    $(".left_promo").click(function(){
	$("#popupNY").fadeIn(350);
    }); // click

    $(".prList .imgCont").each(function(){
        var pr_images = $(">.img-wrap img", this);
        var total_slides = pr_images.length
        pr_images.hide();
        $(pr_images[0]).show();
        var pr_a = $(".pag ul li a", this);
        var pr_li = $(".pag li", this);
        pr_a.each(function(e){
            $(this).click(function(){
                pr_li.removeClass("active");
                $(this.parentNode).addClass("active");
                pr_images.hide();
                $(pr_images[e]).show();
                return false;
                }); // click
            }) // each

        pr_images.each(function(e){
            $(this).click(function(){
                if (e == total_slides-1){
                    $(pr_a[0]).click();
                    }
                else {
                    $(pr_a[e+1]).click();
                    }
                }) // click
            }) // each

        }); // each

  $.reject({
        reject: {
            msie6: true, // Microsoft Internet Explorer
            msie5: true // Microsoft Internet Explorer
        },
        imagePath: 'images/'
    }); // Customized Browsers



    contact_form = $("form");
    if (contact_form.length){
        contact_form.validate({
            rules:{
                message: {
                    required: true,
                    minlength: 18
                },
                subject: {
                    required: true,
                    minlength: 7
                },
                name: {
                    required: true,
                    minlength: 1
                },
                email: {
                    required: true,
                    email: true
                }
            },
            errorPlacement:function(error, element){
                //$(element).focus();
            },
            errorClass:"invalid",
            highlight:function(element, errorClass, validClass){
                $(element).parent().addClass(errorClass).removeClass(validClass);
            },
            unhighlight: function(element, errorClass, validClass){
                $(element).parent().removeClass(errorClass).addClass(validClass);
            },
            submitHandler: function(form) {
                var sb = $("input[type=submit]", form).hide()
                
                $(form).ajaxSubmit({
                    dataType: 'xml',
                    success: function(data){
                        //$('form').hide();
                        $('#for_messages_id').empty();
                        $('#for_messages_id').prepend('<p class="confirmation"><span>'+$("root", data).text()+'</span></p>');
                        $('.confirmation').show();
                        //alert($("root", data).text());
                    }
                });
           }

        }); // submit

        // IE focus
        if ($.browser.msie){
            var all_inputs = $("input,textarea", contact_form);
            all_inputs.focus(function(){$(this).addClass("focused")});
            all_inputs.blur(function(){$(this).removeClass("focused")});
        }

        } // if

        //
        // Active menu
        //
        $.fn.reverse = [].reverse;
        var menu = $("#leftMenu a").reverse();
        var link_found = false;
        menu.each(function(){
            if (!link_found){
                if(window.location.href.indexOf(this.href) == 0 ) {
                    $(this).closest("li").addClass("act");
                    link_found = true;
                } // if location
            } // if link
        }) // each
    // clients map countries
    if( $('body').find("area") ){
        $("area").hover(
        function(){
            show_client($(this).attr('alt'));
        },
        function(){
            $('#clients').empty();
        });
        $('.clients').hover(
            function(){
                show_client($(this).attr('id'));
            },
            function(){
                $('#clients').empty();
            });
    }// end clients map
    // show technologies descriptions
    if( $('body').find('.Tools a img') ){
        $(".Tools a img").hover(
            function(){
                var tt = $(this).parent().find("span.popup");
                var position = $(this).position();
                var x2 = position.top - tt.height() - 25;
                var y2 = position.left + $(this).width()/2 - tt.width()/2;
                tt.css({top: x2, left: y2});
                tt.show();
            },
            function(){
                $("span.popup").fadeOut(150);
        }); // hover
    } // end show technologies

    // show employee name
    if( $('body').find('.team p.iWe a img') ){
        $(".team p.iWe a img").hover(
            function(){
                var tt = $(this).parent().parent().find("span.popup");
                var position = $(this).position();
                var x2 = position.top - tt.height() - 25;
                var y2 = position.left + $(this).width()/2 - tt.width()/2;
                tt.css({top: x2, left: y2});
                tt.show();
            },
            function(){
                $("span.popup").fadeOut(150);
        }); // hover
    } // end show technologies

    // show employee name
    if( $('body').find('.allTeam ul li a img') ){
        $(".allTeam ul li a img").hover(
            function(){
                var tt = $(this).parent().parent().find("span.popup");
                var position = $(this).position();
                var x2 = position.top - tt.height() - 25;
                var y2 = position.left + $(this).width()/2 - tt.width()/2;
                tt.css({top: x2, left: y2});
                tt.show();
            },
            function(){
                $("span.popup").fadeOut(150);
        }); // hover
    } // end show technologies

    // work around 'more projects button'
    if( $('body').find('#more_projects_id') ){
        $('#more_projects_id').click(function(){
            var position = $('#project_list_id').children().last().attr('id');
            $.post($(this).attr('href'), {position: position.substring(4)}, function(data){
                $(data.root).appendTo('#project_list_id');
                if(data.more == 'False'){
                    $('#more_projects_id').closest("p").remove();
                }
            }, 'json');
            return false;
        });
    } // end 'more button'

    // work around 'more products button'
    if( $('body').find('#more_products_id') ){
        $('#more_products_id').click(function(){
            var position = $('#product_list_id').children().last().attr('id');
            $.post($(this).attr('href'), {position: position.substring(4)}, function(data){
                $(data.root).appendTo('#product_list_id');
                if(data.more == 'False'){
                    $('#more_products_id').remove();
                }
            }, 'json');
            return false;
        });
    } // end 'more button'

    // work around 'more open source button'
    if( $('body').find('#more_opensource_id') ){
        $('#more_opensource_id').click(function(){
            var position = $('#opensource_list_id').children().last().attr('id');
            $.post($(this).attr('href'), {position: position.substring(4)}, function(data){
                $(data.root).appendTo('#opensource_list_id');
                if(data.more == 'False'){
                    $('#more_opensource_id').remove();
                }
            }, 'json');
            return false;
        });
    } // end 'more button'
})// document.ready

function show_client(country){
    var country_name = $('#'+country).text();
    $('<img src="/media/images/'+country+'.png" class="'+country+'" id="map_'+country+'" /><span class="popup" style="display:none" id="popup_'+country+'">'+country_name+'</span>').appendTo('#clients');
    var image = $('#map_'+country);
    var popup = $('#popup_'+country);
    var x2 = image.position().top - popup.height() - 25;
    var y2 = image.position().left + image.width()/2 - popup.width()/2;
    popup.css({top:x2, left:y2, position:'absolute'});
    popup.show();
}


