var SDZ = SDZ || {};
var fontsize = 0;
if ($.browser.msie && $.browser.version.substr(0,1)<7) {var ie6=1;}

SDZ.init = $.extend(SDZ.init,{
  'IEHover': function(e) {
    $(e).each(function(){
      var $this = $(this);
      $this.bind('mouseover',function() {$this.addClass('over');}).bind('mouseout',function() {$this.removeClass('over');});
    });
  },
  'Access': function() {
    // Hides fallback functionality
    $(".noscript").hide();
	// Replicate missing IE6 :hover
    if (ie6) {
      SDZ.init.IEHover('#menu li');
      SDZ.init.IEHover('#gallery .asset');
    }
    return true;
  },
  'InputDefault': function(e) {
    // Toggles and fades form input default value
    var dV = [], $this = $(e), $tI = $this.attr('id');
    dV[$tI] = $this.val();
    $this.css("color", '#666').live('focus', function() {
      if ($this.val() == dV[$tI]) {$this.val('');$this.css('color','#000');}
    }).live('blur', function() {
      if ($this.val() == '') {$this.val(dV[$tI]);$this.css('color','#666');}
    });
  },
  'MediaLink' : function(e) {
    $(e).each(function(){
      var $this = $(this);
      $this.bind('click',function(f) {f.preventDefault();SDZ.utils.AjaxOverlay($this.attr('href'),$this.attr('rel').replace(/.*-([^-]+)$/, "$1"),670);});
    });
  },
  'MediaLinkCascaded' : function(e) {
    $(e).each(function(index){
      var $this = $(this);
      $this.bind('click',function(f) {f.preventDefault();SDZ.utils.AjaxOverlay($this.children('a:first').attr('href'),$this.attr('rel').replace(/.*-([^-]+)$/, "$1"),670);}).css('cursor','pointer');
    });
  },
 'HistoryOverlay' : function(e) {
    $(e).each(function(){
      var $this = $(this);
      $this.bind('click',function(f) {
        f.preventDefault();
        SDZ.utils.AddOverlay(601);
        $("#overlay .content").attr('id', 'flashDiv');
        var flashvars = {};
        flashvars.swfPath = "/assets/media/shared/flash/history/swf/main.swf";
        flashvars.xmlPath = "/assets/media/shared/flash/history/xml/data.xml";
        var params = {};
        params.menu = "false";
        params.allowFullscreen = "true";
        params.scale = "noscale";
        params.wmode = "transparent";
        var attributes = {};
        swfobject.embedSWF("/assets/media/shared/flash/history/swf/preloader.swf", "flashDiv", "601", "403", "9.0.0", false, flashvars, params, attributes);
        //$('#overlay').jqmShow();
      });
    });
  },
  'ShareThis' : function(e) {
    $(e).each(function(index){
      var $this = $(this);
      $this.bind('click',function(f) {f.preventDefault();SDZ.utils.AjaxOverlay($this.attr('href'),$this.attr('rel').replace(/.*-([^-]+)$/, "$1"),300);});
    });
  } 
});

SDZ.utils = $.extend(SDZ.utils,{
  'AjaxOverlay' : function(url,type,width) {
    SDZ.utils.AddOverlay(width);
    // Dynamically load content
    $("#overlay .content").load(url + ' #main', function(response, status, xhr) {
      if (status == "error") {
        document.location = url;
      } else {
          $("a[rel='video-youtube']").ytchromeless();
      } 
    });
  },
  'AddOverlay' : function(width) {
    $('#overlay').remove();
    $('body').append('<div class="overlay-window" id="overlay" style="width:'+width+'px;margin-left:-'+width/2+'px"><a href="#" class="overlay-close" title="Click to close overlay">Close</a><div class="content">Loading...</div></div>');
    $('#overlay').jqm({
        overlayClass: 'overlay-bg',
        closeClass: 'overlay-close',
        overlay: 90,
        onHide: SDZ.RemoveOverlay
    });
    $('#overlay').jqmShow();
  },
  'RemoveOverlay' : function() {
    $('#overlay').remove();
  },
  'Print' : function() {
  var html = $('*').html().replace(/media\="?print"?/, "media\=\"all\"");
  var printSdz = window.open("","printpreview","");
	printSdz.document.open();
	printSdz.document.write(html);
	printSdz.document.close();
	printSdz.print();
  },  
  'Fontsize' : function() {
  	if (fontsize==0) {
		SDZ.utils.LoadStyle('fontsize_large');
		fontsize=1;
  	} else if (fontsize==1) {
		$('#fontsize_large').remove();
  		SDZ.utils.LoadStyle('fontsize_xlarge');
		fontsize=2;
  	} else {
		$('#fontsize_xlarge').remove();
		fontsize=0;
  	}
  },  
  'LoadStyle' : function(type) {
    $('<link>').appendTo('head').attr({
      id: type, 
      rel: 'stylesheet',
      type: 'text/css',
      href: '/assets/layout/css/'+type+'.css'
    });
  }
});

SDZ.gadgets = $.extend(SDZ.gadgets,{
  'Dropdown': function(e,d) {
    var $l = $(e);
    $l.children('ul.noscript').before('<form><ol><li><select name="selectLocation"><option value="">'+$(e + ' h4').text()+':</option></select></li><li><input type="submit" value="Submit Query" class="button-access white"></li></ol></form>');
    var $s = $(e + ' select');
    $(e + ' ul.noscript li a').each(function(){
      $s.append('<option value="'+$(this).attr('href')+'">'+$(this).text()+'</option>');
    });
    $(e + ' form').submit(function() {
      var v = $(this).find(':selected').val();
      if (v!='') {
        if (d!=1) {
          if (v.match(/^http/)) {
            window.open(v);
          } else {
            $(window.location).attr('href', v);
          }
        } else {
          SDZ.utils.AddOverlay(450);
          $("#overlay .content").html($(e + ' .disclaimer').html());
          $("#overlay .content .access").removeClass('access');
          $("#overlay .content a.go").attr("href",$(this).find(':selected').val()).live('click',function(){$('#overlay').jqmHide();});
          $("#overlay .content a.cancel").live('click',function(){$('#overlay').jqmHide();});
        }
      }
      return false;
    });
  },
  'Websites': function(e) {
    return true;
  },
  'Buttons': function(e) {
    $(".gadget .button-access").replaceWith('<input type="image" src="/assets/layout/img/button_round_arrow_w'+cImg+'" alt="Search" name="confirm" class="button">');
  }
});

jQuery.validator.addMethod("phone", function(value, element) { 
  return this.optional(element) || /^[0-9\-\.\/\(\)\+]+/.test(value); 
}, "Please provide a valid phone number using digits and + . / ( ) -");


$(document).ready(function($) {
  SDZ.init.Access(); 
  SDZ.init.InputDefault('#search .txt');
  SDZ.gadgets.Dropdown('.gadget.locations',0);
  SDZ.gadgets.Dropdown('.gadget.websites',1);
  SDZ.init.InputDefault('.gadget.products .txt');
  SDZ.init.MediaLink("a[rel^='overlay'],[rel^='overlay'] a");
  SDZ.init.MediaLinkCascaded("#gallery [rel^='overlay']");
  SDZ.init.HistoryOverlay("a[rel^='sandoz-history']");
  SDZ.init.ShareThis("a[rel='share-page']");
  // Activate accordions
  $(".accordion").accordion({ 'header' : '.header', 'autoheight': false, 'collapsible': true, 'active': false, 'clearStyle': true });
  $("a[rel='video-youtube']").ytchromeless();
  $("#webform").validate({
    success: function() {
        var msg = '';
        $("#webform").attr("action","https://www.apps.sandoz.com/sws1/send.pub");
        $("#webform dl input, #webform dl textarea").each(function(){
          var $this = $(this);
          var fieldId = $this.attr('id');
          var label = $("#webform dl label[for='"+fieldId+"']").html();
          msg += '<h3>' + label + '</h3><p>' + $this.val() + '</p>';
        });
        $("#webform input[name='sms.form.view']").val(msg);
      }
  });
});




