/**
* medesign: EIGENE JAVASCRIPT ROUTINEN (basierend auf jquery)
* und SKYPE BUTTON JS
*
*/


/* JS-Funktionen losfeuern wenn das Dokument bereit ist. */
$(document).ready( function()
{
   //IMGREP.rollover.init();
   //PRODUCTS.getId.popup();
   
   // table cart form
   PRODUCTS.getId.init();
   
   //png fix
   //jQuery(function(){$('div#page').pngFix( { blankgif:'clear.gif' } );});
});


// ---------------------------------------------------------------------------------

/* SHOP TABELLEN UM WARENKORB LINK ERGÄNZEN */
/* tables must be of class 'productTablePessary' */
PRODUCTS = {};
PRODUCTS.getId =
{
   init: function()
   {
        this.preload();
   },

   /*mouseover popup */
   popup: function()
   {
        jQuery("td.tableCart").hover(
            function(){
                $(this).addClass("hoverCart");
            },
            function() {
                $(this).removeClass("hoverCart");
            }
        );
   },
   
   preload: function()
   {
      jQuery(window).bind('load', function() {
          //check for language we are in (defalt = en)
          var language = $('meta[http-equiv=content-language]').attr("content");
          if (language == 'de' ) {
              var url = 'http://www.medesign.de/search/result';
              var linktitle = 'Im Online Shop suchen (neues Fenster)';
          }
          else {
            var url = 'http://www.medesign.de/search/result';
            var linktitle = 'Search for product in online shop (new window)';            
          }
            
          //loop through the rows
          jQuery(".productTablePessary tr").each(function() {
            if (this.rowIndex == 0) { // first row=header 
                $(".productTablePessary .tr-0").append("<th>Shop</th>");
            }
            else {
                //get product id in row
                productId = $(this).find("td").eq(3).html(); 
                //append table cell (form with url and product id)
                if(productId != "") {
                    //$(this).append("<td class='tableCart'><form method='POST' class='productForm' target='medesign Shop' action='" + url + "'> <input type='hidden' name='anzahl' value='1'><input type='hidden' name='artikel_nr' value='" + productId + "'><input type='submit' name='cart' value='cart' class='cart' onClick='' title='" + linktitle + "'/> </form></td>");
		    $(this).append("<td class='tableCart'><form method='POST' class='productForm' target='medesign Shop' action='" + url + "'> <input type='hidden' name='suchbegriff_normal' value='" + productId + "'><input type='submit' name='cart' value='cart' class='cart' onClick='' title='" + linktitle + "'/> </form></td>");                    
                }
            }
          });
      });
   }
};


// ---------------------------------------------------------------------------------

/* HEADER LOGO MOUSEOVER  (image replacement mit bild suffix _ro bei allen img class="rollover") NOT USED*/
IMGREP = {};
IMGREP.rollover =
{
   init: function()
   {
      this.preload();
     
      jQuery(".headImg1").hover(
         function () { $(this).attr( 'src', IMGREP.rollover.newimage($(this).attr('src')) ); },
         function () { $(this).attr( 'src', IMGREP.rollover.oldimage($(this).attr('src')) ); }
      );
      
   },

   preload: function()
   {
      jQuery(window).bind('load', function() {
         $('.headImg1').each( function( key, elm ) { $('<img>').attr( 'src', IMGREP.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src )
   {
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_ro' + src.match(/(\.[a-z]+)$/)[0];
   },

   oldimage: function( src )
   {
      return src.replace(/_ro\./, '.');
   }
};



// ---------------------------------------------------------------------------------

/** 
* SKYPE.JS für SKYPE BUTTON IM FOOTER
 */
var activex = ((navigator.userAgent.indexOf('Win')  != -1) && (navigator.userAgent.indexOf('MSIE') != -1) && (parseInt(navigator.appVersion) >= 4 ));
var CantDetect = ((navigator.userAgent.indexOf('Safari')  != -1) || (navigator.userAgent.indexOf('Opera')  != -1));

function oopsPopup() {
    if((navigator.language && navigator.language.indexOf("ja") != -1) || (navigator.systemLanguage && navigator.systemLanguage.indexOf("ja") != -1) || (navigator.userLanguage && navigator.userLanguage.indexOf("ja") != -1)) {
        var URLtoOpen = "http://download.skype.com/share/skypebuttons/oops/oops_ja.html";
    } else {
        var URLtoOpen = "http://download.skype.com/share/skypebuttons/oops/oops.html";
    }
	var windowName = "oops";
	var popW = 540, popH = 305;
	var scrollB = 'no';
	w = screen.availWidth;
	h = screen.availHeight;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	oopswindow = window.open(URLtoOpen, windowName,'width=' + popW + ',height=' + popH + ',scrollbars=' + scrollB + ',screenx=' +leftPos +',screeny=' +topPos +',top=' +topPos +',left=' +leftPos);
	return false;
}

if(typeof(detected) == "undefined" && activex) {
    document.write(
        ['<script language="VBscript">',
        'Function isSkypeInstalled()',
        'on error resume next',
        'Set oSkype = CreateObject("Skype.Detection")',
        'isSkypeInstalled = IsObject(oSkype)',
        'Set oSkype = nothing',
        'End Function',
        '</script>'].join("\n")
    );
}

function skypeCheck() {
    if(CantDetect) {
        return true;
    } else if(!activex) {
        var skypeMime = navigator.mimeTypes["application/x-skype"];
        detected = true;
        if(typeof(skypeMime) == "object") {
            return true;
        } else {
            return oopsPopup();
        }
    } else {
        if(isSkypeInstalled()) {
            detected = true;
            return true;
        }
    }
    
    detected = true;
    return oopsPopup();
}

function loadDetection() {
    if(document.getElementById && document.getElementsByTagName) {
        if (window.addEventListener) window.addEventListener('load', addDetection, false);
        else if (window.attachEvent) window.attachEvent('onload', addDetection);
    }
}

function addDetection() {
    var pageLinks = document.getElementsByTagName("a");
    for (var i=0; i < pageLinks.length; i++) {
        if(pageLinks[i].childNodes[0] && pageLinks[i].childNodes[0].src) {
            if((pageLinks[i].childNodes[0].src.indexOf('download.skype.com\/share\/skypebuttons') != -1 || pageLinks[i].childNodes[0].src.indexOf('mystatus.skype.com') != -1) && (typeof(pageLinks[i].onclick) == "undefined" || pageLinks[i].onclick == null)) {
                pageLinks[i].onclick = function sChk() { return skypeCheck(); }
            }
        }
    }
}

loadDetection();


// ---------------------------------------------------------------------------------



/** 
* HINTERGRUND AN FENSTERGRÖSSE ANPASSEN 
* NOT USED
 */
function adjust_window() {
  var width = jQuery(document).width();
  var height = jQuery(document).height();
	/*colWidth = (width - 845)/2;
  if (colWidth > 394) { colWidth = 394 }*/
  
  if (height < 560) {
    attribute = 560;
    jQuery("#main").css("min-height", attribute );
   // jQuery("#main").css("min-height", attribute );
  }
  else {
    attribute = height;
	//attribute = 'block';
    jQuery(document).height(attribute );	
    //jQuery("#main").css("min-height", attribute ); 
  }
};


//$(document).ready(adjust_window);
//$(window).resize(adjust_window);
