function check(target, id)
{
    // Regular Expression matching from
	// http://regexlib.com/UserPatterns.aspx?authorId=1758
    var emailRegExp = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/i;
        
    if(target.val().length == 0)
    {
        return false;
    }
    
    if(id == "email" && !emailRegExp.test(target.val()))
    {
        return false;
    }
        
    return true;
}


function resetForm()
{
    var elements = new Array("name", "email", "issue");
    for(var i = 0;i<elements.length;i++)
    {
        $("#"+elements[i]).css("border-color", "#333");
        $("#"+elements[i]).val("");
    }
}

function onSubmit()
{
    var Name = $("#name");
    var Email = $("#email");
    var Issue = $("#issue");
        
    var formAction = $("#contact_form").attr("action");
    
    var toValidate = new Array("name", "email", "issue");
    
    var haveErrors = false;
    var errorsOn = new Array();
    
    for(var i = 0;i<toValidate.length;i++)
    {
        var id = toValidate[i];
        var elem = $("#"+id);
        var result = check(elem, id);
        if(!result)
        {
            haveErrors = true;
            errorsOn[errorsOn.length] = id;
            elem.css("border-color", "#b30000");
        }
        else
        {
            elem.css("border-color", "#333");
        }
    }
    
    
    if(haveErrors == true)
    {
        var text = "";
        for(var i = 0;i<errorsOn.length;i++)
        {
            var str = errorsOn[i];
            text += "  - " + str.slice(0, 1).toUpperCase() + str.slice(1, str.length)+"\n";
        }
        alert("Please, check your input in:\n" + text, "Error");
    }
    else
    {
        $.post(formAction,
               {name: Name.val(), email: Email.val(), issue: Issue.val()},
               function(data)
               {
                   if(data == 1)
                   {
                       alert("Your mail has been sent!!!");
                   }
                   else
                   {
                       alert("Sorry, your mail could not be sent.\nThere is a problem with our Mail Service by now.\nPlease, try again later or just call us.");
                   }
                   resetForm();
               }
              );
        
    }

    return false;
}

function fixFontsSize()
{
    var platform = window.navigator.platform;
    platform = platform.slice(0, 3).toLowerCase();
    if(platform == "win")
    {
	    $("blockquote").css("font-family", "Times New Roman").css("font-size", "26px");
    	$("#sidebar a, blockquote.long_quote").css("font-family", "Times New Roman").css("font-size", "18px");
    	$("#main p").css("margin-top", "10px").css("margin-bottom", "13px");
    }
    
}

function hideGalleryThumbnail()
{
    $("#thumbnail_displayer").css(
				  {
				   "display" : "none",
                                   "position": "static",
                                   "height"  : "0",
                                   "width"   : "0",
                                   "top"     : "0",
                                   "left"    : "0"
				  }
				 );
    
    $("#thumbnail_displayer").unbind("mouseout");
}


function showGalleryThumbnail()
{
    hideGalleryThumbnail();
    
    var img = $(this).find("img");
    var offset = $(this).parent().offset();
    
    var boxWidth = $(this).width();
    var boxHeight = $(this).height();
    
    var ratio = img.attr("width")/img.attr("height");
    
    if(ratio >= 1)
    {
        var imgWidth = $("#preview_width").attr("value");
        var imgHeight = Math.round(imgWidth/ratio);
    }
    else
    {
        var imgHeight = $("#preview_height").attr("value");
        var imgWidth = Math.round(imgHeight*ratio);
    }
    
    var top = Math.round(offset.top + (boxHeight/2) - (imgHeight/2))
    var left = Math.round(offset.left + (boxWidth/2) - (imgWidth/2));
    
    if(top <= 0) { top = Math.round(offset.top); }
    if((Math.round(left)+Math.round(imgWidth)) > $(document).width()) { left = Math.round(offset.left) + Math.round(boxWidth) - Math.round(imgWidth); }
    if((left) <= 0) { left = Math.round(offset.left); }
    
    $("#thumbnail_displayer").css(
                                  {
                                    "height"  : imgHeight + "px",
                                    "width"   : imgWidth  + "px",
                                    "position": "absolute",
                                    "top"     : top       + "px",
                                    "left"    : left      + "px"
                                   }
                                  );

    $("#thumbnail_displayer").html($(this).parent().attr("innerHTML")+"</img>");
    
    $("#thumbnail_displayer a img").css(
                                  {
                                    "height"     : imgHeight + "px",
                                    "width"      : imgWidth  + "px",
                                    "margin-top" : "0"
                                   }
                                  );
    
    $("#thumbnail_displayer").mouseout(hideGalleryThumbnail);
    
    $("#thumbnail_displayer").fadeIn(500);
    
    $("#gallery ul.thumbnails a").unbind("mouseover");
    $("#gallery ul.thumbnails a").mouseover(showGalleryThumbnail);
}

/*
function hideHowToBuy()
{
    $("#how_to_buy_displayer").css(
				  {
				   "display" : "none",
                   "position": "static",
                   "height"  : "0",
                   "width"   : "0",
                   "top"     : "0",
                   "left"    : "0"
				  }
				 );

	return false;
}


function showHowToBuy()
{
    hideHowToBuy();
    
    var offset = $(this).parent().offset();
    
    var width = 280;
    var height = 130;
    var top = offset.top;
    var left = offset.left;
        
    $("#how_to_buy_displayer").css(
                                  {
                                    "padding"   : "5px",
                                    "width"   : width  + "px",
                                    "height"   : height  + "px",
                                    "position": "absolute",
                                    "top"     : top       + "px",
                                    "left"    : left      + "px",
                                    "background-color"     : "white",
                                    "border"     : "2px solid #c9c9c9"
                                   }
                                  );
    
    $("#how_to_buy_displayer").show(500);
    
    return false;
}
*/

function fixBodyHeight()
{
    if($("#body").height() < $(window).height())
    {
        $("#body").height($(window).height());
        var footerTop = $(window).height() - $("#footer").height() - $("#footer").offset().top;
        $("#footer").css("position", "relative").css("top", footerTop);
    }
}


function atReady()
{
    $("#contact_form").submit(onSubmit);
    $("#gallery ul.thumbnails a").mouseover(showGalleryThumbnail);
    /*$("#how_to_buy").click(showHowToBuy);
    $("#how_to_buy_close").click(hideHowToBuy);
    fixFontsSize();*/
    
    fixBodyHeight();
    $(window).resize(fixBodyHeight);
}

$(document).ready(atReady)


