/**
 * @author Laurentiu Ciovica (laurentiu.ciovica@gmail.com ||
 *         http://l2-studios.com)
 */
var $j = jQuery.noConflict();
$j(document).ready(function() {
  initContact();
  initBriefing();
  initGalery();
  initActivities();
  initNews();
  initDownload();
  initPartners();
  var hits = $j('#hits').val();
  var length = hits.length;
  for ( var i = 0; i < length; i++) {
    $j('#d' + (6 - (length - (i + 1)))).html(hits.charAt(i));
  }
});

function initContact() {
  $j('#contactDialog').hide();
  $j('.contact-holder').bind('click', showContact);
  $j('#contactDialog').dialog( {
    autoOpen : false,
    bgiframe : true,
    modal : true,
    resizable : false,
    position : 'top',
    width : 820,
    height : 570
  });
}
function initNews() {
  $j('#newsDialog').hide();
  $j('.news-holder').bind('click', showNews);
  $j('#newsDialog').dialog( {
    autoOpen : false,
    bgiframe : true,
    modal : true,
    resizable : false,
    position : 'center',
    width : 480,
    height : 400
  });
}
function initBriefing() {
  $j('#briefingDialog').hide();
  $j('.briefing-holder').bind('click', showBriefing);
  $j('#briefingDialog').dialog( {
    autoOpen : false,
    bgiframe : true,
    modal : true,
    resizable : false,
    position : 'top',
    width : 800,
    height : 550
  });
}
function initActivities() {
  $j('#activitiesDialog').hide();
  $j('.activities-holder').bind('click', showActivities);
  $j('#activitiesDialog').dialog( {
    autoOpen : false,
    bgiframe : true,
    modal : true,
    resizable : false,
    position : 'top',
    width : 800,
    height : 550
  });
}
function initDownload() {
  $j('#downloadDialog').hide();
  $j('.download-holder').bind('click', showDownload);
  $j('#downloadDialog').dialog( {
    autoOpen : false,
    bgiframe : true,
    modal : true,
    resizable : false,
    position : 'center',
    width : 500,
    height : 450
  });
}
function initPartners() {
  $j('#partnersDialog').hide();
  $j('.partners-holder').bind('click', showPartners);
  $j('#partnersDialog').dialog( {
    autoOpen : false,
    bgiframe : true,
    modal : true,
    resizable : false,
    position : 'top',
    width : 400,
    height : 550
  });
}
function initGalery() {
  $j('#galeryDialog').hide();
  $j('.galery-holder').bind('click', showGalery);
  $j('#galeryDialog').dialog( {
    autoOpen : false,
    bgiframe : true,
    modal : true,
    resizable : false,
    position : 'center',
    width : 800,
    height : 200
  });
}

function showContact() {
  $j('#contactDialog iframe').remove();
  $j('#contactDialog').append($j(createIFrame($j('#cd-url').val()))).dialog('open');
}
function showBriefing() {
  $j('#briefingDialog iframe').remove();
  $j('#briefingDialog').append($j(createIFrame($j('#bd-url').val()))).dialog('open');
}
function showActivities() {
  $j('#activitiesDialog iframe').remove();
  $j('#activitiesDialog').append($j(createIFrame($j('#ad-url').val()))).dialog('open');
}
function showGalery() {
  $j('#galeryDialog').dialog('open');
}
function showDownload() {
  $j('#downloadDialog iframe').remove();
  $j('#downloadDialog').append($j(createIFrame($j('#dd-url').val()))).dialog('open');
}
function showNews() {
  $j('#newsDialog iframe').remove();
  $j('#newsDialog').append($j(createIFrame($j('#nd-url').val()))).dialog('open');
}
function showPartners() {
  $j('#partnersDialog iframe').remove();
  $j('#partnersDialog').append($j(createIFrame($j('#pd-url').val()))).dialog('open');
}
function subscribe(e) {
  e.preventDefault();
  var email = $j('.subscriber-mail').val();
  var valid = true;
  if (email == '' || validateEmail(email) == false) {
    alert('Adresa de email trebuie s\u0103 fie valid\u0103.');
    valid = false;
  }
  if (valid) {
    var action = $j('.actionUrl').val();
    $j.postJSON(action, {
      email : email,
      ajax : 'true'
    }, function(jsonData) {
      if (jsonData.result != 'success') {
        alert('Adresa dvs de email nu a putut fi salvat\u0103!');
      }
    });
  }
}

function createIFrame(url) {
  var iframe = $j('<iframe />').attr( {
    'frameborder' : '0',
    'src' : url
  }).css( {
    'width' : '100%',
    'height' : '100%',
    'border' : '0'
  });
  return iframe;
}

function redirect2Home(e) {
  document.location.href = $j('#home-ro').attr('href');
}
function validateEmail(email) {
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  if (reg.test(email) == false) {
    return false;
  }
}
