function openContent(_url)
{             
    _ext = _url.search('ext');
    
    if (_ext > 0) {        
        $("#content-modal").fadeIn(500, function () {
            if (_url) {
                loadContent(_url, 'modal');            
            }                   
        });            
    }
    else {
                
        $("#content").fadeIn(500, function () {
            if (_url) {
                loadContent(_url);            
            }                   
        });                    
    }

}

function loadContent(_url, _type)
{   
    if (_type == 'modal') {
                
        $("#loading-modal").show();	 
            
        $.ajax( {
            url: _url,
            cache: false,          
            success: function(html) {
                
                $("#loading-modal").hide();
                $("#bodytext-modal").html(html);
                
                $("a").click(function() {    
                    if ($(this).attr("href") != "#") {                        
                        if ($(this).attr("target") == "_blank") {                
                            return true;
                        }      
                        if ($(this).attr("target") == "_self") {
                            return true;
                        }                               	               
                        else {
                            loadContent($(this).attr("href"));
                        }
                    }
                    return false;
                });         
            }
        });                    
    }
    
    
    else {        
                 
        $("#loading").show();	 
            
        $.ajax( {
            url: _url,
            cache: false,          
            success: function(html) {
                
                $("#loading").hide();
                $("#bodytext").html(html);
                
                $("a").click(function() {    
                    if ($(this).attr("href") != "#") {                        
                        if ($(this).attr("target") == "_blank") {                
                            return true;
                        }
                        if ($(this).attr("target") == "_self") {
                            return true;
                        }                           
                        else if ($(this).attr("rel") == "tb") {
                            return false;
                        }        	 
                        else if ($(this).attr("rel") == "popup") {
                            return false;
                        }              	               
                        else {
                            loadContent($(this).attr("href"));
                        }
                    }
                    return false;
                });         
                           
                tb_init('a.thickbox');
                imgLoader = new Image();
                imgLoader.src = tb_pathToImage;   
                
                var is_ie = $.browser.msie;
                var br_version = Math.round($.browser.version);
                
                if (is_ie && br_version > 6) {                
                    // disable superfish
                }
                else {
                    if ( $(".navg").length > 0 )
                    {
                    	$(".navg").superfish({
                    		animation : { opacity:"show" }
                    	});
                    }                 
                }
                
            }
        });                
    }        
}

		    
function closeContent(_type)
{
    if (_type == 'modal') {
        $("#content-modal").fadeOut(500, function () {
            $("#bodytext-modal").html('');
        });                
    }
    else {
        $("#content").fadeOut(500, function () {
            $("#bodytext").html('');
        });
    }        
    document.navi.wakeFlash();
}

function contactForm(_form)
{
    if(notEmpty(_form.yht_nimi, "name"))
    {
        if(notEmpty(_form.yht_teksti, "message"))
        {
            _form.laheta.disabled = true;
            _form.laheta.value = 'Wait...';
            doPost(_form, '/en/contact/post_contact/');
        }
    }
   return false;
}
function contactFormFI(_form)
{
    if(notEmptyFI(_form.yht_nimi, "nimi"))
    {
        if(notEmptyFI(_form.yht_teksti, "viesti"))
        {
            _form.laheta.disabled = true;
            _form.laheta.value = 'Odota...';
            doPost(_form, '/fi/yhteys/post_contact/');
        }
    }
   return false;
}

function ecardForm(_form)
{
    if(notEmpty(_form.email, "recipient email address"))
    {
        if(notEmpty(_form.sender, "sender name"))
        {
            _form.laheta.disabled = true;
            _form.laheta.value = 'Wait...';
            doPostE(_form, '/en/e-cards/');
        }
    }
   return false;
}

function ecardFormFI(_form)
{
    if(notEmptyFI(_form.email, "vastaanottajan sähköpostiosoite"))
    {
        if(notEmptyFI(_form.sender, "lähettäjän nimi"))
        {
            _form.laheta.disabled = true;
            _form.laheta.value = 'Odota...';
            doPostE(_form, '/fi/e-kortit/');
        }
    }
   return false;
}

function notEmpty(elem, n)
{
   var str = elem.value;
   if(str.length == 0)
   {
      alert("Please fill in the " + n + " field.");
      elem.focus();
      return false;
   } 
   else 
   {
      return true;
   }
}

function notEmptyFI(elem, n)
{
   var str = elem.value;
   if(str.length == 0)
   {
      alert("Kirjoita vielä " + n + ".");
      elem.focus();
      return false;
   } 
   else 
   {
      return true;
   }
}

function doPost(_form, _url)
{
    var post_yht_nimi = $("#yht_nimi").val();
    var post_yht_email = $("#yht_email").val();
    var post_yht_teksti = $("#yht_teksti").val();
    var post_yht_vo = jQuery('select :selected').val();

    var post_action =  $("#action").val();   

    $.post(_url, { yht_nimi: post_yht_nimi, yht_email: post_yht_email, yht_teksti: post_yht_teksti, yht_vastaanottaja: post_yht_vo, action: post_action },
      function(data) {
        window.location='#top';
        if (data == 'ok') {       
            if (_url == '/fi/yhteys/post_contact/') {
                $("#contact-form").html('<h3>L&auml;het&auml; viesti Lovexille</h3><p style="margin-top: 5px;font-weight: bold;">Viestisi on l&auml;hetetty!</p><p>Sulje t&auml;m&auml; ikkuna ja avaa se uudelleen, jos haluat viel&auml; l&auml;hett&auml;&auml; viestej&auml;.</p>');   
            }
            else {
                $("#contact-form").html('<h3>Send a message</h3><p style="margin-top: 5px;font-weight: bold;">Your message was sent successfully!</p><p>Please close and open this window again to reload contact form.</p>');   
            }
        }
        else {
            if (_url == '/fi/yhteys/post_contact/') {
                $("#contact-form").html('<h3>L&auml;het&auml; viesti Lovexille</h3><p style="margin-top: 5px;font-weight: bold;">Viestin l&auml;hetys ep&auml;onnistui! Yrit&auml; my&ouml;hemmin uudelleen.</p>');   
            }
            else {            
                $("#contact-form").html('<h3>Send a message</h3><p style="margin-top: 5px;font-weight: bold;">Your message could not be delivered. Please try again later.</p>');   
            }
        }
      });        
    return false;
}

function doPostE(_form, _url)
{    
    var post_ecard = $("input[@name='ecard']:checked").val();
    var post_email = $("#email").val();
    var post_sender = $("#sender").val();

    var post_action =  $("#action").val();   

    $.post(_url, { email: post_email, ecard: post_ecard, sender: post_sender, action: post_action },
      function(data) {
        window.location='#top';
        if (data == 'ok') {       
            if (_url == '/fi/e-kortit/') {
                $("#ecard-form").html('<p style="font-weight: bold;">Kortti on l&auml;hetetty!</p><p>Sulje t&auml;m&auml; ikkuna ja avaa se uudelleen, jos haluat viel&auml; l&auml;hett&auml;&auml; kortteja.</p>');   
            }
            else {
                $("#ecard-form").html('<p style="font-weight: bold;">The selected e-card was sent successfully!</p><p>Please close and open this window again to reload form.</p>');   
            }
        }
        else {
            if (_url == '/fi/e-kortit/') {
                $("#ecard-form").html('<p style="font-weight: bold;">Kortin l&auml;hetys ep&auml;onnistui! Yrit&auml; my&ouml;hemmin uudelleen.</p>');   
            }
            else {            
                $("#ecard-form").html('<p style="font-weight: bold;">Your card could not be delivered. Please try again later.</p>');   
            }
        }
      });        
    return false;
}

$(document).ready(function()
{
    $("#close-window").click(function() {
        closeContent();
    }); 
    $("#close-window-modal").click(function() {
        closeContent('modal');
    }); 
    
    $("#loading").html('<img src="/img/load_anim.gif" width="24" height="24" alt="" />');    
    $("#loading").hide();
    $("#loading-modal").html('<img src="/img/load_anim.gif" width="24" height="24" alt="" />');    
    $("#loading-modal").hide();    
    
}); 

window.onerror = function() {
    return true;
};