/**
 * 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;
var direction = '';
var modifikation = 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();



	if(FlashResizer.headerType == 'headonly') {
		modifikation = 0;
		$('#text-teaser>.text-teaser_item').css('padding-top', '23px');
	} else {
		modifikation = 0;
		$('#text-teaser>.text-teaser_item').css('padding-top', '23px');

		if($('#breadnav').length > 0) {
			$('#main').css('top', '62px');
		}
	}

	$('div#text-teaser').height(65-modifikation);

	$('div#text-teaser').bind('click',function(){
		toggleTeaser();
	});

	$('div.teaserOpen').bind('click',function(){
		toggleTeaser();
	});

	$('div.teaserClose').bind('click',function(){
		toggleTeaser();
	});

	resizeMainDiv();

	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');
	}
}

function ShowPop(element) {
	document.getElementById(element).style.display='block';
}

function HiddenPop(element) {
	document.getElementById(element).style.display='none';
}

function toggleTeaser()
{
	if ($('div#text-teaser').height() == 65-modifikation) {
		$('div.teaserOpen').hide();
		direction = 'up';
		gotoHeight = 197-modifikation;
		$('#text-teaser').css('right', '0px');
		$('div#text-teaser').animate({ "height": gotoHeight }, 'slow', function(){
			if (($.browser.name == 'msie' && $.browser.versionNumber == '6') && (FlashResizer.wmodetransparent == 0 && FlashResizer.scaleFlash == 1)) {
				var higher = (startingHight - $('#text-teaser').height()) + 60-modifikation;
				$('#teaserflash>embed').attr('height', higher);
				$('#teaserflash').height(higher);
				$('#main').height(higher);
			}

			$('div.teaserClose').show();
		});
	} else {
		$('div.teaserClose').hide();
		direction = 'down';
		gotoHeight = 65-modifikation;
		$('div#text-teaser').animate({ "height": gotoHeight }, 'slow', function(){
			if(($.browser.name == 'msie' && $.browser.versionNumber == '6') && (FlashResizer.wmodetransparent == 0 && FlashResizer.scaleFlash == 1)) {
				var higher = (startingHight-$('#text-teaser').height()) + 60-modifikation;
				$('#teaserflash>embed').attr('height',higher);
				$('#teaserflash').height(higher);
				$('#main').height(higher);
			}

			$('div.teaserOpen').show();
			$('#text-teaser').css('right', '17px');
		});
	}
}

function clickTeaserLink(link) {
	$('div#text-teaser').unbind();
	window.location = link;
}

var default_pos_main = 32; // aus main.css
var default_pos_main_offset = 0;
var resize1st = true;

function resizeMainDiv()
{
	if(isFullFlash) { // Wenn komplett Flash dann keine Anpassung
		$('#text-teaser').css('right', 0);
		if ($('#breadnav').length > 0) {
			$('#main').height(($(window).height() - default_pos_main_offset) - 130);
		}
	} else {
		if(resize1st) {
			// die Standard-Position aus der main.css wird abgezogen,
			// der Wert hat somit 0 - ausser bei einer weiteren CSS-Anpassung
			// wird der Wert geändert, z.B. Abstand im Finder.
			// Die Differenz wird auch von der main_height abgezogen.
			default_pos_main_offset = $('#main').css('top').replace(/px/, '') - default_pos_main;
			resize1st = false;
		}
		new_main_top = default_pos_main_offset;
		new_main_height = $(window).height() - default_pos_main_offset;

		if($('#nav_main').height()) {
			var navmain_offset = 8; // Feinanpassung sichtbarer Bereich
			new_main_height -= ($('#nav_main').height() - navmain_offset);
			new_main_top += ($('#nav_main').height() - navmain_offset);
		}

		if($('#breadnav').height()) {
			var breadnav_offset = 4; // Feinanpassung sichtbarer Bereich
			new_main_height -= ($('#breadnav').height() - breadnav_offset);
			new_main_top += ($('#breadnav').height() - breadnav_offset);
		}

		if($('#text-teaser').height()) {
			new_main_height -= ($('#text-teaser').height() - 25); // Offset weil sichtbarer Element-Bereich kleiner
		}

		if($('#footer').height()) {
			new_main_height -= $('#footer').height();
		}

		$('#main').height(new_main_height);
		$('#main').css('top', new_main_top);
		$('#main').css('padding', 0);
	}

	FlashResizer.resize();
}

/* : EVENTS : */
$(window).resize(function(){
	if (FlashResizer.scaleFlash == 1 && FlashResizer.wmodetransparent == 0) {
		FlashResizer.maxHeight = ($(window).height() - 103);
		FlashResizer.minHeight = ($(window).height() - 103);
	}

	resizeMainDiv();

	if($('#main').height() < ($('#main').attr('scrollHeight') - $('#main').css('padding-top').replace(/px/, ''))) {
		$('#logo').css('right', '20px');
		$('#nav_buttons').css('margin-right', '130px');
		if($('div.go').length > 0) {
			$('div.go').css('margin-left', '11px');
		}
	} else {
		$('#logo').css('right', '10px');
		$('#nav_buttons').css('margin-right', '120px');
		if($('div.go').length > 0) {
			$('div.go').css('margin-left', '21px');
		}
	}
});



function include( filename ) {
    // http://kevin.vanzonneveld.net
    // +   original by: mdsjack (http://www.mdsjack.bo.it)
    // +   improved by: Legaev Andrey
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Michael White (http://getsprink.com)
    // %        note 1: Force Javascript execution to pause until the file is loaded. Usually causes failure if the file never loads. ( Use sparingly! )
    // %        note 2: The included file does not come available until a second script block, so typically use this in the header.
    // *     example 1: include('http://www.phpjs.org/js/phpjs/_supporters/pj_test_supportfile_2.js');
    // *     returns 1: 1

    var js = document.createElement('script');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', filename);
    js.setAttribute('defer', 'defer');
    document.getElementsByTagName('HEAD')[0].appendChild(js);

    // save include state for reference by include_once
    var cur_file = {};
    cur_file[window.location.href] = 1;

    if (!window.php_js) window.php_js = {};
    if (!window.php_js.includes) window.php_js.includes = cur_file;
    if (!window.php_js.includes[filename]) {
        window.php_js.includes[filename] = 1;
    } else {
        window.php_js.includes[filename]++;
    }

    return window.php_js.includes[filename];
}

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');
			});
		}
	});
}

function include_once( filename ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Legaev Andrey
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Michael White (http://getsprink.com)
    // -    depends on: include
    // *     example 1: include_once('http://www.phpjs.org/js/phpjs/_supporters/pj_test_supportfile_2.js');
    // *     returns 1: true

    var cur_file = {};
    cur_file[window.location.href] = 1;

    if (!window.php_js) window.php_js = {};
    if (!window.php_js.includes) window.php_js.includes = cur_file;
    if (!window.php_js.includes[filename]) {
        if(include(filename)){
            return true;
        }
    } else{
        return true;
    }
}

function popen(url) {
	myWindow = window.open(url, 'popen', 'width=1000,height=700,resizable=yes,scrollbars=yes');
	myWindow.focus();
	return false;
}



/* : uhlsport FC : */

$(document).ready(function() {

	// on load page
	render_fc_bargain();
	render_fc_suggest();
	render_fc_add2leafletposition();

	// on resize page
	//document.body.onresize = function (){ render_fc_bargain(); }
	$(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 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(size, price) {
	add2leaflet_size = size;
	add2leaflet_price = price;
	$('span#fcChangePrice').text(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');
}
