//not used anymore
function expand_sign_up_form()
{
    jQuery('#sign_up_form').animate({height: 400},500);
} 
//not used anymore
function expand_sign_up_form_site()
{
    jQuery('#sign_up_form_site').animate({height: 400},500);
} 
//not used anymore
function expand_sign_up_form_site_fr()
{
    jQuery('#sign_up_form_site_fr').animate({height: 400},500);
} 

//used in the home page only
function expand_subscribe_form()
{
    if(jQuery('#Address').val() == '' || jQuery('#Address').val() == undefined)
     {
	    alert('You must enter a valid e-mail address to continue');
     }
    else
	{
	    jQuery('#subscribe_container').animate({height: 400},500);
	}
} 

// wait for the DOM to be loaded 
function collapse_sign_up_form()
{
    jQuery('#sign_up_form').animate({height: 110},500);
} 

// wait for the DOM to be loaded 
function collapse_sign_up_form_site()
{
    jQuery('#sign_up_form_site').animate({height: 110},500);
} 
// wait for the DOM to be loaded 

jQuery(document).ready(function() {
	       jQuery('#article_image').jScale({w:'309px'});
            if(jQuery('.ad_banner img').width >= 309){  jQuery('.ad_banner img').jScale({w:'309px'});}  
              if(jQuery('.main_content_banner_ad_container img').width >= 582){jQuery('.main_content_banner_ad_container img').jScale({w:'582px'});}
              jQuery('#parties_story_banner').jScale({w:'474px'});
               jQuery('.gallery_image').jScale({w:'100px'});
           //    scale_thumbnail_to_fit('.gallery_thumbnail', 69);

//                 jQuery('img').each(function(){ if( jQuery(this).attr('src') == '/' ) {jQuery(this).attr('src', '/assets/images/1x1.png'); jQuery(this).width('1px'); jQuery(this).height('1px');  } });

	       if(jQuery.client.os == "Windows" && jQuery.client.browser == "Firefox")
		   {
		       var head = document.getElementsByTagName('head')[0]; 
		       var css_href = "/assets/css/chicsetera_firefox_win.css";
		       jQuery(document.createElement('link')).attr({
			       type: 'text/css', 
			       href: css_href, 
			       rel: "stylesheet"}).appendTo(head);
		   }
               jQuery.validator.messages.required = "";
               jQuery("#form_subscribe").validate({invalidHandler: function(e, validator) 
                                                                      {
                                                                         var errors = validator.numberOfInvalids();
			                                                             if (errors) {
				                                                         var message = errors == 1
                                                                         ? 'You missed 1 field. It has been highlighted below'
                                                                         : 'You missed ' + errors + ' fields.  They have been highlighted below';
                                                                         $("div.error span").html(message);
				                                                         $("div.error").show();
                                                                         } else {
                                                                         $("div.error").hide();
			                                                             }
                                                                      }, errorElement: "span"
                                                  });
	
    });


function submit_sign_up_form()
{
    
    show_loader();
    // bind to the form's submit event
    jQuery('#subscription_form').ajaxSubmit({ 
	    success: showResponse,
	    type: 'post' });
}
function submit_sign_up_form_site()
{
    
    show_loader();
    // bind to the form's submit event
    jQuery('#subscription_form_site').ajaxSubmit({ 
	    success: showResponse,
	    type: 'post' });
}

// post-submit callback 
function showResponse(responseText, statusText)  { 

    
    hide_loader();
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
    if(responseText != 'Success')
    {
	alert(responseText);
    }
    else
    {
	jQuery('#subscription_form').clearForm();
	collapse_sign_up_form();
	alert('Thank you for signing up');
    }
} 

// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
}
function show_loader()
{
    jQuery('#content_loading').fadeIn(5);
}
function hide_loader()
{
    jQuery('#content_loading').hide();
}

function scale_thumbnail_to_fit(selector, max_size)
{

    jQuery(selector).each(function(){
                                                          
                                                              if(this.width < this.height)
                                                              {
                                                                   jQuery(this).jScale({w: max_size + 'px'});
                                                              }
                                                             else 
                                                            {
                                                                       jQuery(this).jScale({h: max_size + 'px'});
                                                             }
                                                            }
);
}

function go_to_subscribe_with_email()
{
    //get the value of the field and go to the location
    var email = document.getElementById('Address').value;
    location.href='/en/subscribe?email=' + email;
}

function go_to_subscribe_with_email_fr()
{
    //get the value of the field and go to the location
    var email = document.getElementById('Address').value;
    location.href='/fr/inscription?email=' + email;
}

function get_url_param( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function fill_in_email_value()
{
    document.getElementById('Address').value = get_url_param('email');
} 