$(document).ready(function(){
    if( $("input[value='votes']") )
    	$("input[value='votes']").parent().parent().remove();
    if( $("input[value='score']") )
    	$("input[value='score']").parent().parent().remove();
    if( $("input[value='mark']") )
    	$("input[value='mark']").parent().parent().remove();
    if( $("input[value='preview']") )
    	$("input[value='preview']").parent().parent().remove();
	
    if( $("#help-contact") ) {
        $("#help-contact").click(function(){
            
            window.open(this.href, 'HelpAndContact', 'width=600, height=750,menubar=no, location=no, toolbar=no, directories=no, status, resizable=0, scrollbars=no');
            return false;
            
        });
    }

    if( $("input[name='typeId']") ) {
        $("input[name='typeId']").click( function() {
            var element = $(this);
            var mark_type_id = element.val();
            var project_id = $("#markProjectId").val();
            $.get("/admin/index.asp", { method: "markProject", projectId: project_id, markTypeId: mark_type_id },
                function(data){
                    $("#text-mark").html("You have tagged this project as <strong class=\"\">" + data + "</strong><img src=\"/site/images/mark_type_" + String(mark_type_id) + ".gif\" />");
                }
            );
        });
    }

     /* VIDEOS */
     if( $(".video-link") ) {
         $(".video-link").each( function( i, video ) {
            $(video).attr("href","javascript:void(0)");
         });
    }
    
     if( $("#sub img").length ) {
           
        var sub_element = $("#sub");
        var img_movement = 96;
        var img_number_visible = 6;
        var images = $("#sub img");
        var img_number = images.length;
        var slide_duration = 250;
    
        //It moves the sub element 'img_movement' px to the left
        $("#next_page").bind("click", function() {
            var max_offset = -(img_number - img_number_visible) * img_movement;
            var offset = 0;
            if( parseInt(sub_element.css("left")) )
                offset = parseInt(sub_element.css("left"));
            
            if( offset != max_offset ) {
                sub_element.animate( { left: "-="+img_movement }, slide_duration );
            }
        });
        
        //It moves the sub element 'img_movement' px to the right
        $("#prev_page").bind("click", function() {
            var offset = parseInt(sub_element.css("left"));
            if( offset != 0 ) {
                sub_element.animate( { left: "-=-"+img_movement }, slide_duration );
            }
        });
    
    }

    if( $("a.gallery-link") ) {
        $("a.gallery-link").fancybox({
            'transitionIn'	:	'elastic',
            'transitionOut'	:	'elastic',
            'speedIn'		:	600, 
            'speedOut'		:	200, 
            'overlayShow'	:	true,
            'overlayOpacity':   0.5
        });
    }

    if( $('#main-video').length ) {      
        // IE
        changeWMode();
    }

    if( $('ul.share-icons li a img').length ) {
        $('ul.share-icons li a img').hover(function() {
            $('ul.share-icons li a img').css("opacity","1");
            $(this).css("opacity","0.6");
        });        
        $('ul.share-icons li a img').mouseout(function() {
            $('ul.share-icons li a img').css("opacity","1");
        });
    }

    if( $('img.play-link').length ) {
        $('img.play-link').hover(function() {
            $('img.play').css("opacity","1");
            $(this).prev().css("opacity","0.6");
        });        
        $('img.play-link').mouseout(function() {
            $('img.play').css("opacity","1");
        });

        $(".videos-container ul li a").click(function() {
            var video_id = $(this).attr("id");
            $("#main-video").html('<img class="loading" src="/site/images/load.gif" alt="Loading..." />');
            var url = "/site/ajax/video_by_id.asp?id=" + video_id;
            $.getJSON(url, function(data) {
//                var json = $.parseJSON(data);
                $("#video-title-paragaph").text(data.title);
                $("#main-video").html(data.embed);
                setTimeout("changeWMode()",200);
            });
        });

    }

    if( $(".future-title").length ) {
        $(".future-title").css("opacity","0.7");
    }

    var text = "<p>Submission of projects has been closed according to the contest rules.</p>";
    text += "<p>Thank you very much for Jumping the Gap!</p>";
    text += "<p>During the next days, a pre-selection committee made up of representatives of the Roca Innovation Lab will select 30 projects from amongst the entries received, which will be presented to the jury. We will keep you informed.</p>";
    text += "<p>Don't forget to regularly visit Jumpthegap 2.0. in Facebook (Join in) and find all the news and information on this contest and much more!!</p>";
    text += "<p>Good luck!!</p>";
    $("#clock").countdown({
        until: new Date(2011,3,29,13,0,0),
//        layout:"<p class=\"time\"><span>{dn}</span> <span class=\"large\">days</span> <span>{hn}h</span> <span>{mn}'</span> <span>{snn}''</span></p><p> TO CLOSE THE SUBMISSION OF PROJECTS<br />4TH EDITION OF JUMPTHEGAP</p>"
        layout:"<p class=\"time\"><span>{dn}</span> <span class=\"large\">days</span> <span>{hn}h</span> <span>{mn}'</span> <span>{snn}''</span></p>" + text
    });

    if( $("a.close-clock").length ) {
        $("a.close-clock").attr("href","#");
    }

    $("body.cd").click(function(){
        $("a.close-clock").hide();
        $("#countdown").hide();
        $("body").removeClass("cd");
    });

    $("a.close-clock").click(function(){
        $("#countdown").hide();
        $("a.close-clock").hide();
        $("body").removeClass("cd");
    });

    blinkOutLastMonth();



    // Finalists 2011 //
    var currentProjectMouseOut = null;
    var currentProjectMouseOver = null;
    var overlayer = false;
    if( $("#finalists-container div.project").length ) {
        $("#finalists-container div.project").mouseover(function() {
            currentProjectMouseOut = null;
            currentProjectMouseOver = $(this);            
            $(this).children(".info").hide();
            // Show image project
            $(this).children(".project-image").show();
            $(this).css("position","relative");
            if(!$(this).hasClass("last"))
                $(this).css("border-right-color","#6da3d6");
            $(this).css("border-bottom-color","#6da3d6");
            $(this).css("z-index","100");
            if( !overlayer ) {
                overlayer = true;
                $("#finalists-container-overlayer").fadeTo(135, 0.7, function() {
                    $("#finalists-container-overlayer").show();
                    overlayer = false;
                });
            }
        });
        $("#finalists-container div.project").mouseout(function() {
            currentProjectMouseOver = null;
            currentProjectMouseOut = $(this);
            if( !overlayer ) {
                overlayer = true;
                $("#finalists-container-overlayer").fadeTo(75, 0, function() {
                    $("#finalists-container-overlayer").hide();
                    overlayer = false;
                });
            }
            if(!$(this).hasClass("last"))
                $(this).css("border-right-color","#fff");
            $(this).css("border-bottom-color","#fff");
            // Hide image project
            $(this).children(".project-image").hide();
            $(this).children(".info").show();
            $(this).css("position","");
            $(this).css("z-index","1");
            
        });
    }

    checkMousePointer = function() {
        var disp = $("#finalists-container-overlayer").css("display");
        if( !currentProjectMouseOver && String(disp) != "none" ) {
            $("#finalists-container-overlayer").fadeTo(200, 0, function() {
                $("#finalists-container-overlayer").hide();
            });
        }
        setTimeout(checkMousePointer,2000);
    }

    checkMousePointer();

    ////////////////////
	
});

blinkOutLastMonth = function() {
    $('ul.calendar li.selected').css({
        "border-bottom": "5px solid #66A7D4"
    });
    setTimeout(blinkInLastMonth,500);
}

blinkInLastMonth = function() {
    $('ul.calendar li.selected').css({
            "border-bottom": "5px solid #006DB8"
    });
    setTimeout(blinkOutLastMonth,500);
}

changeWMode = function() {
    var embedTag;
    $("embed").each(function(i) {
        embedTag = $(this).attr("outerHTML");
        if ((embedTag != null) && (embedTag.length > 0)) {
            embedTag = embedTag.replace(/embed /gi, "embed wmode=\"opaque\" ");
            $(this).attr("outerHTML", embedTag);
        }
        // This "else" was added
        else {
            $(this).wrap("<div></div>");
        }
    });
}

