/*** Common Helpers ***/

function createCookie(name, value, days)
{
  if (days)
  {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var expires = "; expires=" + date.toGMTString();
  }
  else
  {
    var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
  }
}

/*** Search Citywire ***/
function SearchCitywire()
{
  var searchTerm = $('div#searchBar input#keywords').val();
  
  // Crazy awesome regex to extract the app root and edition out of a url
  var urlRegex = new RegExp("^.{1,}?\/\/.{1,}?\/(Citywire\.Web\/)?(adviser|personal|professional|selector)\/.*$", "i");
  var rgxResult = urlRegex.exec(window.location);
  
  var appRoot = rgxResult[1] ? rgxResult[1] : "";
  var edition = rgxResult[2];
  
  var searchUrl = "/" + appRoot + edition + "/search.aspx?phrase=" + searchTerm;
  
  window.location = searchUrl;
}

/*** Open legal text in a popup ***/
function LegalWindow(path, name)
{
  window.open(path, name, 'width=500,height=600,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=0');
  return false;
}



/*** END Common Helpers***/




/*** Stuff that happens after the page has loaded ***/
$(function() {


    // Move header before img to achieve better layout and adjust floating and padding values
    $('div.column-1 div.box img.headlineImage + h2').each(function() {
        var h2 = $(this);
        var img = h2.prev();

        img.insertAfter(h2);
    });


    // Search box watermarking
    $('.cwsearchWatermark').focus(function() {
        $(this).removeClass('cwsearchWatermark');
    }).blur(function() {
        if ($(this).val() == '') {
            $(this).addClass('cwsearchWatermark');
        }
    });


    // FTSE Chart switching
    $('#ddlftsemrktdata').change(function() {
        var ftseid = $(this).val();

        $('.ftsemrktdata:visible').fadeOut("normal", function() {
            $('#' + ftseid).fadeIn("normal");
        });
    });


    // Promotion box slider (relies on jQuery CodaSlider plugin)
    $(".promotionBox-slider").codaSlider();

    // Promotion box cycling (relies on jQuery Cycle Plugin)
    $(".promotionBox").cycle({
        pause: 1,       // pause on hover
        timeout: 5000   // 5 seconds per item
    });

    // scrollable boxes (relies on jquery tools)
    $("div.scrollable").scrollable({
        size: 3
    });
    
    // Capture enter press on the search box
    $("input#keywords").keyup(function(evt) {
      if (evt.keyCode == 13) {
        evt.preventDefault();
        SearchCitywire();
      }
    });
});



/*** For website v2 ***/
function WriteScreenRes() {
  createCookie("sr", screen.width + '|' + screen.height, 365);
}

WriteScreenRes();
//*** For website v2 ***/

