/**
 * Uhlsport default.js
 * -------------------------------------------------------------------------
 * @author Weitclick
 * @copyright Copyright(C) 2009, Weitcklick
 * @version $Id: default.js 22 2009-02-23 13:52:40Z ma $
 * @revision $LastChangedRevision:0 $
 **/

/* : GLOBALS : */
var strLastOpenBigBreadNav = '';
var startingHight = 0;

/* : FUNKTIONEN : */
function InitAll() {
  if($.browser.name == "msie") {
    fixButtonsIE();
  }
	
  $('a').click(function() {
    this.blur();
  });

  /*$.ifixpng('/fileadmin/templates/img/shared/transpix.gif');
  $('img[rel="fixpng"]').ifixpng();
  $('dt>img').ifixpng();*/
	
  $('div#text-teaser').bind('click',function(){
    toggleTeaser();
  });
	
  $('div.teaserOpen').bind('click',function(){
    toggleTeaser();
  });
	
  $('div.teaserClose').bind('click',function(){
    toggleTeaser();
  });
	
  if($('#main').height() < ($('#main').attr('scrollHeight') - $('#main').css('padding-top').replace(/px/, ''))) {
    $('#logo').css('right', '20px');
    $('#nav_buttons').css('margin-right', '150px');
    if($('div.go').length > 0) {
      $('div.go').css('margin-left', '11px');
    }
  } else {
    $('#logo').css('right', '10px');
    $('#nav_buttons').css('margin-right', '140px');
    if($('div.go').length > 0) {
      $('div.go').css('margin-left', '21px');
    }
  }
	
  if($.browser.name == "firefox" && $.browser.versionNumber == "2") {
    $('div#breadnav').css('z-index','0');
  }
	
  if($.browser.name == "firefox" && $.browser.versionNumber == "3") {
    $('#nav_buttons>a>button>div').css('line-height','1px');
    $('button').css('padding-bottom','5px');
  }
	
  if ($.browser.name == 'msie' && $.browser.versionNumber == '6') {
    $('div#breadnav').css('background', 'url(fileadmin/templates/img/shared/breadCrump_zu.gif) left bottom no-repeat');
  }

  resizeMain();
}

$(window).resize(function() {
  resizeMain();  
});

function resizeMain() {
  var mainHeight = $('body').height();
  mainHeight = mainHeight - $('#nav_main').height();

  if($('#breadnav').length) {
    mainHeight = mainHeight - $('#breadnav').height();

    var nmtop = $('#breadnav').position().top + $('#breadnav').height();
    $('#main').css('top', nmtop);
  }

  mainHeight = mainHeight - $('#text-teaser').height();

  $('#main').height(mainHeight);
}

function ShowPop(element) {
  document.getElementById(element).style.display='block';
}

function HiddenPop(element) {
  document.getElementById(element).style.display='none';
}

var teaserOpen = false;
var teaserCloseHeight = 65;
var teaserOpenHeight = 197;

function toggleTeaser()
{
  var mainHeight = $('#main').height();


  if(teaserOpen) {
    $('div.teaserClose').hide();
    $('#text-teaser').animate(
    {
      height: teaserCloseHeight
    },
    {
      duration: '20',
      step: function( currentHeight ){
        var dist = currentHeight - teaserOpenHeight;
        $('#main').height(mainHeight - dist);
      },
      complete: function() {
        $('div.teaserOpen').show();
        teaserOpen = false;
      }
    });
  } else {
    $('div.teaserOpen').hide();
    $('#text-teaser').animate(
    {
      height: teaserOpenHeight
    },
    {
      duration: '20',
      step: function( currentHeight ){
        var dist = currentHeight - teaserCloseHeight;
        $('#main').height(mainHeight - dist);
      },
      complete: function() {
        $('div.teaserClose').show();
        teaserOpen = true;
      }
    });
  }
}

function clickTeaserLink(link) {
  $('div#text-teaser').unbind();
  window.location = link;
}

function fixButtonsIE()
{
  var objButtonList	= $('a>button');

  objButtonList.each(function(i){
    if($(this).parent().attr('href') != 'javascript:void(0);' && $(this).parent().attr('href') != 'undefined') {
      $(this).bind('click', function(){
        window.location = $(this).parent().attr('href');
      });
    }
  });
}


/* : uhlsport FC : */
$(document).ready(function() {

  // on load page
  render_fc_bargain();
  render_fc_suggest();
  render_fc_add2leafletposition();
	
  // on resize page
  $(window).resize(function() {
    render_fc_bargain();
    render_fc_suggest();
    render_fc_add2leafletposition();
  });

  // ------------------------------------------

  $('div#nav_login_form').find('input.inputlogin').css('width',192);
	
  $('div#nav_login').click(function() {
    logincall();
  });
	
  $('div.login_form_submit').click(function() {
    $('form#form_nav_fc_login').submit();
  });

  $('form#form_nav_fc_login').submit(function() {
    $(this).hide();
    $('div#loggin_in_process').show();
    $(this).find('label').removeClass('error');
		
    $.post($('a#fc_login_link').attr('href'), $(this).serialize(), function(data) {
      if (data['stat']=='ok') {
        window.location.href = $('a#fc_login_link_success').attr('href');
      } else {
        if (data['msg']=='login') {
          $('label.jLoginMsg').addClass('error');
        } else {
          $('label.jLoginMsg_'+data['msg']).addClass('error');
        }
				
        $('div#loggin_in_process').hide();
        $('form#form_nav_fc_login').show();
				
        fc_shake_login();
				
      }
    }, 'json');
  });
	
  $('div#nav_logout').click(function() {
    $.post($('a#fc_login_link').attr('href'), {
      type:'logout'
    }, function(data) {
      if (data['stat']=='ok') {
        window.location.href = $('a.jLogoutLink').attr('href');
      }
    }, 'json');
  });
	
  $('div.jNavLeaflet').click(function() {
    window.location.href = $(this).find('a').attr('href');
  });
	
  $('div.jSubmitRegistration').click(function() {
    $('div.checkNotice').hide();
    $('input').removeClass('errorinput');
    $('select').removeClass('errorinput');
    $('label').removeClass('errortext');
		
    $.post($('form#form_fc_register').attr('action'), $('form#form_fc_register').serialize(), function(data) {
      if (data['stat']=='ok') {
        window.location.href = $('a.jSuccessfulRegistration').attr('href');
      } else {
        fc_refreshcaptcha( $('form#form_fc_register').attr('action') );
        window.location.href = window.location.href.replace('#top','') + '#top';
        $('div.checkNotice').show();
        for (i=0; i<data['msg'].length; i++) {
          $('input[name='+data['msg'][i]+']').addClass('errorinput');
          $('select[name='+data['msg'][i]+']').addClass('errorinput');
          $('label[for=reg_'+data['msg'][i]+']').addClass('errortext');
        }
      }
    },'json');
  });

  $('div.jSubmitUpdate').click(function() {
    $('div.jCheckInput').hide();
    $('div.jUpdateOk').hide();
    $('input').removeClass('errorinput');
    $('select').removeClass('errorinput');
    $('label').removeClass('errortext');
		
    $.post($('form#form_fc_profile').attr('action'), $('form#form_fc_profile').serialize(), function(data) {
      if (data['stat']=='ok') {
        $(document).attr('location').hash = '#top';
        $('div.jUpdateOk').show();
      } else {
        $(document).attr('location').hash = '#top';
        $('div.jCheckInput').show();
        for (i=0; i<data['msg'].length; i++) {
          $('input[name='+data['msg'][i]+']').addClass('errorinput');
          $('select[name='+data['msg'][i]+']').addClass('errorinput');
          $('label[for=reg_'+data['msg'][i]+']').addClass('errortext');
        }
      }
    },'json');
  });
	
  $('img.fc_suggest_next').click(function() {
    moveSuggest(-1);
  });
	
  $('img.fc_suggest_prev').click(function() {
    moveSuggest(1);
  });
	
	
	
  $('div.jAdd2leaflet').click(function() {
    $.post('/fileadmin/uhlsport/php/fc_leaflet.php', {
      t:'add',
      articlefromcatalog:add2leaflet_articlefromcatalog,
      itemNumber:add2leaflet_itemNumber,
      size:add2leaflet_size
    }, function(data) {
      if (data!='false') {
        $('span#leaflet_counter').text(data);
				
        //var left = parseInt($('div#nav_login_form').css('left'));
				
        $('div#fc_added2leaflet').css({
          'opacity':'0'
        }).show()
        .animate({
          'top':'+=10',
          'opacity':'100'
        }, {
          queue:true,
          duration:500
        })
        .animate({
          'top':'-=10'
        }, {
          queue:true,
          duration:500
        })
        .animate({
          'top':'+=10'
        }, {
          queue:true,
          duration:500
        })
        .animate({
          'top':'-=10'
        }, {
          queue:true,
          duration:500
        })
        .animate({
          'top':'+=10',
          'opacity':'0'
        }, {
          queue:true,
          duration:500
        });
      }
    });
  })

	
	
  $('a.jRemoveFromLeaflet').click(function() {
    $.post('/fileadmin/uhlsport/php/fc_leaflet.php', $('form#form_fc_leaflet').serialize(), function(data) {
      if (data['stat']=='ok') {
        window.location.reload();
      }
    },'json');
  });

		
  $('a.jFCRequestNewPassword').click(function() {
		
    $(this).find('label').removeClass('error');
		
    $.post($('a#fc_login_link').attr('href'), {
      type:'password',
      email:$('input#form_login_input_email').val()
    }, function(data) {
			
      if (data['stat']=='error') {
        fc_shake_login();
        $('label.jLoginMsg_'+data['msg']).addClass('error');
      } else {
        $('div#loggin_newpassword').show();
        $('form#form_nav_fc_login').hide();
      }
			
    },'json');
  });
	
	
  $('img.jReloadLeafletQuantity').click(function() {
    var id = $(this).attr('alt');
		
    $.post('/fileadmin/uhlsport/php/fc_leaflet.php', {
      t:'reload',
      id:id,
      quantity:$('input[name=quantity'+id+']').val()
    }, function(data) {
      if (data['stat']=='ok') {
        window.location.reload();
      }
    },'json');
  });

  $('div.fc_pricecensored').bind('mouseenter', function(e) {
    $('body').bind('mousemove', function(e) {
      var left	= e.pageX;
      var top		= e.pageY;

      left	= left - ($('div#fc_teaser_mouseover').width() / 2);
      top		= top + 15;

      $('div#fc_teaser_mouseover').css({
        'left':left,
        'top':top
      }).show();
    });
  });

	
	
  $('div.fc_pricecensored').bind('mouseleave', function() {
    $('body').unbind('mousemove');
    $('div#fc_teaser_mouseover').hide();
  });
	
	
	
  function moveSuggest(direction) {
    var left = parseInt($('div.suggestcontent').css('left'));
    var width_holder = $('div.suggestholder').width();
    var width_suggest = $('div.suggestcontent').width();
    var new_position = left+direction*$('div.fc_suggest_article').width();
		
    // check weite content rechtsbündig
    if (direction==-1 && width_suggest<((new_position*-1)+width_holder) ) {
      new_position = -1*(width_suggest-width_holder);
    } else if (direction==1 && new_position>0 ) {
      new_position = 0;
    }
		
    $('div.suggestcontent').animate({
      'left':new_position
    }, '250');
  }
});

function popen(url) {
	myWindow = window.open(url, 'popen', 'width=1000,height=700,resizable=yes,scrollbars=yes');
	myWindow.focus();
	return false;
}


function render_fc_add2leafletposition() {
  if ($('div.jNavLeaflet')[0]) {
    var left = parseInt($('div.jNavLeaflet').position().left);
    var width = parseInt($('div.jNavLeaflet').width());
    $('div#fc_added2leaflet').css({
      'left':left-((169-width-10)/2)
    });
  }
}

function render_fc_bargain() {
  var fc_bargain_width = $('div#fc_bargain').width() - 16;
  $('div.fc_bargain_teaser').css({
    'width':fc_bargain_width/4-1
  });
  $('div#fc_bargain_left').width( fc_bargain_width/2-1 );
  $('div#fc_bargain_right').width( fc_bargain_width/2-1 );
  $('div.fc_bargain_teaser_holder').css({
    'margin-left':($('div.fc_bargain_teaser').width()-$('div.fc_bargain_teaser_holder').width())/2
  });
}

function render_fc_suggest() {
  var count_teasersections = 7;
  var fc_suggest_width = $('div.suggestholder').width() - 16;
  $('div.fc_suggest_article').css({
    'width':Math.floor(fc_suggest_width/4)
  });
  $('div.suggestcontent').css({
    'width':$('div.fc_suggest_article').width()*count_teasersections+count_teasersections
  });
  $('div.fc_suggest_content').css({
    'margin-left':($('div.fc_suggest_article').width()-$('div.fc_suggest_content').width())/2
  });
}

function fc_selectAllFromLeaflet() {
  $('input.jRemoveFromLeaflet').attr('checked', true);
}

function selectSizeAndPrice(value) {
  var valueAr = value.split('|');
  add2leaflet_size = valueAr[0];
  add2leaflet_price = valueAr[1];
  $('span#fcChangePrice').text(add2leaflet_price);
}

function selectSizeAndPriceBargain(size, price, oldprice, percent) {
  selectSizeAndPrice(size, price);
  $('span#fcChangeOldPrice').text(oldprice);
  $('span#fcChangePercent').text(percent);
}

function logincall() {
  var position = $('div#nav_login').position();
  var left = position.left + parseInt($('div#nav_login').css('margin-left')) - 35;
  $('div#nav_login_form').css('left', left).toggle();
}

function fc_shake_login() {
  var left = parseInt($('div#nav_login_form').css('left'));
	
  $('div#nav_login_form')
  .animate({
    'left':left-10
  }, {
    queue:true,
    duration:50
  })
  .animate({
    'left':left+10
  }, {
    queue:true,
    duration:50
  })
  .animate({
    'left':left-10
  }, {
    queue:true,
    duration:50
  })
  .animate({
    'left':left+10
  }, {
    queue:true,
    duration:50
  })
  .animate({
    'left':left-10
  }, {
    queue:true,
    duration:50
  })
  .animate({
    'left':left
  }, {
    queue:true,
    duration:50
  })
;
}



function fc_printLeaflet() {
  $.post('/fileadmin/uhlsport/php/fc_leaflet.php', {
    t:'print'
  }, function(data) {
		
    },'json');
}

function fc_refreshcaptcha(url) {
  $.post(url, {
    newcaptcha:'set'
  }, function(data) {
    $('img#fc_captcha').attr('src', '/fileadmin/uhlsport/php/captcha/captcha_image.php?img='+data['captcha']);
    $('input#fc_captcha_input').val('');
    $('input#fc_captcha_session').val(data['session']);
  }, 'json');
}
