var $j = jQuery.noConflict();

/* redefine content > click on element show clicked item in view in content */
var titleShowContent = {
  init: function(options) {
    var defaults = {
        parentClass: 0,
        showElement : 0,
        contentClass : 0,
        selected : 0
    };
    var options = $j.extend(defaults, options);
    
    if( $j(options.parentClass).length > 0 ){
      //get content to be cloned ie bodytext or textpic
      $j(options.contentClass+' '+options.showElement).remove();
      
      //get first in list
      var el = $j(options.parentClass).eq(0);
      el.addClass(options.selected); 
      
      $j(options.contentClass).append($j(options.showElement, el).clone())
  
      $j(options.parentClass).click( function(){
        $j(options.contentClass+' '+options.showElement).remove();
        $j(options.showElement).removeClass(options.selected);
        $j(options.contentClass).append($j(options.showElement, $j(this)).clone())
        $j(this).addClass(options.selected);
      });
    }
  }
}

Shadowbox.init();

$j(document).ready(function() {   
  if( $j('.ref').length > 0) { 
    titleShowContent.init({
      parentClass: '.ref',
      showElement : '.csc-textpic',
      contentClass : '#content',
      selected : 'selected'
    });
  }
  
  if( $j('.shop').length > 0) {  
    $j('.shop .csc-default').removeAttr('style');
         
    $j('.shop').each(function(s){
    	var $shop = $j(this);
    	
    	$shop.find('img').each(function(i){
//console.log(s);
    	  var $el = $j(this).parent();
          if(!$el.is('a')){ 
            var $img = $el.find('img');
            var hrf = $img.attr('src');
         
            if($img.attr('longdesc') != undefined){
             //console.log($img.attr('longdesc'), hrf);
              hrf = $img.attr('longdesc');
            }
            $img.wrap('<a rel="shadowbox[sh'+s+']" href="'+hrf+'"></a>');
          } else {
            var $img = $el.find('img');
            var hrf = $img.attr('src')
            if($img.attr('longdesc') != undefined){
              hrf = $img.attr('longdesc');
            }
          
            $el.attr({
              'rel':'shadowbox[sh'+s+']',
              'href':hrf
            });
          }
    	});
    	$j(this).find('.csc-textpic-imagewrap li:first').show();
    }); 

    Shadowbox.setup(".shop a", {
      gallery: "sh"
    });
  }
  
  // This is more like it!
  $j('#language a').updateLanguageSwitchURL();
});

/**
 * language switch
 */
(function($) {
  $.fn.updateLanguageSwitchURL = function(){
    Essentials.setURLdetails();
    var myURL = Essentials.getURLdetails();
    var i = myURL.segments.length;
    var el = $(this);
   
    el.click(function(){
        var seg = '';
        if( $(this).attr('id') == 'en'){
            if (myURL.segments[0] == 'en'){
               return false;
            } else {
               seg = 'en/'+myURL.segments[i-1];
            }
        } else {
            if ((myURL.segments[0] == 'en') && (i > 1)){
              seg = myURL.segments[i - 1];
            } else if ((myURL.segments[0] == 'en') && (i == 1)){
                seg = '';
            } else {
                return false;
            }
        }
        
        location.href = 'http://'+myURL.host + '/' + seg;
        return false;
    });
  };
})(jQuery);

Essentials = {
    compatOldBrowser : false ,
    siteURL: null,
    setCompatOldBrowser : function (){
        if ($j.browser.msie && $j.browser.version.substr(0,1)<7) {
              this.compatOldBrowser = true;
          }
        return null;
    },
    getCompatOldBrowser : function (){
        return this.compatOldBrowser;
    },   
    debugCompatOldBrowser : function(){
        alert( this.getCompatOldBrowser() );
    },
    setURLdetails: function(){
        this.siteURL = this.parseURL(document.location.href);
    },
    getURLdetails: function(){
        return this.siteURL;
    },
    // This function creates a new anchor element and uses location
    // properties (inherent) to get the desired URL data. Some String
    // operations are used (to normalize results across browsers).
    parseURL: function (url) {
        var a =  document.createElement('a');
        a.href = url;
        return {
            source: url,
            protocol: a.protocol.replace(':',''),
            host: a.hostname,
            port: a.port,
            query: a.search,
            params: (function(){
            var ret = {},
            seg = a.search.replace(/^\?/,'').split('&'),
            len = seg.length, i = 0, s;
            for (;i<len;i++) {
                if (!seg[i]) { continue; }
                    s = seg[i].split('=');
                    ret[ unescape(s[0]) ] = s[1];
                }
                return ret;
            })(),
            file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
            hash: a.hash.replace('#',''),
            path: a.pathname.replace(/^([^\/])/,'/$1'),
            relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
            segments: a.pathname.replace(/^\//,'').split('/')
        };
    }
};

