var cookie_expire = new Date();
cookie_expire = new Date(cookie_expire.getTime()+1000*60*60*24*14);

/* COOKIE FUNCTIONS                                                  */
/*********************************************************************/

function testcookie(){
	document.cookie = 'testcookie=true';
	if(get_cookie_value('testcookie') != 'true'){
		window.location.href = 'login.php?action=cookies';
	}
}

function get_cookie_value(cookiename){
	var cookie_part_start = document.cookie.indexOf(cookiename);
	if(cookie_part_start == -1){
		var output = '';
	} else {
		var cookie_part_end = document.cookie.indexOf(';', cookie_part_start);
		if (cookie_part_end == -1){cookie_part_end = document.cookie.length;}
		var cookie_content = unescape(document.cookie.substring(cookie_part_start, cookie_part_end));
		
		var cookie_value_start = cookie_content.indexOf('=') + 1;
		var cookie_value_end = cookie_content.indexOf(';', cookie_value_start);
		if (cookie_value_end == -1){cookie_value_end = cookie_content.length;}
		var output = unescape(cookie_content.substring(cookie_value_start, cookie_value_end));
	}
	return output;
}

function set_language(input, expires){
	if(expires == true){
		document.cookie = 'c_lang=' + input + '; expires=' + cookie_expire.toGMTString();
	} else {
		document.cookie = 'c_lang=' + input;
	}
	location.reload();
}

/* BROWSER RESIZE FUNCTIONS                                          */
/*********************************************************************/

function window_resize(){
	var current_width = $(window).width();
	if(current_width < 1000){
		$('.content_wrap').css('left','0');
		$('.content_wrap').css('margin','0px 0px 0px 30px');
	} else {
		$('.content_wrap').css('left','50%');
		$('.content_wrap').css('margin','0px 0px 0px -452px');
	}
}

/* PAGE ELEMENT FUNCTIONS                                            */
/*********************************************************************/

function link_rollover_init(){
	function link_rollover(object,offset){
		if(offset == null){var offset = 0;}
		var link_rollover_newpos = parseInt($(object).css('height')) + offset;
		$(object).css('backgroundPosition','0px -' + link_rollover_newpos + 'px');
	}
	function link_rollout(object,offset){
		$(object).css('backgroundPosition','0px 0px');
	}
	$('.link').mouseover(function(){link_rollover(this,2)});
	$('.link').mouseout(function(){link_rollout(this,2)});
	
	$('.member_list').mouseover(function(){link_rollover(this,6)});
	$('.member_list').mouseout(function(){link_rollout(this,6)});
}

function register_init(){
	// Apply first form content function
	function register_branch_apply(){
		$('#register_shop_name').val($('#register_company_name').val());
		$('#register_shop_email').val($('#register_company_email').val());
		$('#register_shop_website').val($('#register_company_website').val());
		$('#register_shop_address').val($('#register_company_address').val());
		$('#register_shop_zip').val($('#register_company_zip').val());
		$('#register_shop_city').val($('#register_company_city').val());
		$('#register_shop_country').val($('#register_company_country').val());
		$('#register_shop_phone').val($('#register_company_phone').val());
		$('#register_shop_fax').val($('#register_company_fax').val());
		$('#register_contact_name').val($('#register_personal_name').val());
		$('#register_contact_email').val($('#register_personal_email').val());
	}
	
	// Initializing the links
	$('#apply_form').click(function(){register_branch_apply();});
}

function slidebox_home_init(){
	// Change content_wrap's size to the contents needs
	var slidebox_content_box_wrap_size = $('.slidebox_content_box').size() * (parseInt($('.slidebox_content_box').css('width')) + parseInt($('.slidebox_content_box').css('marginRight')));
	$('.slidebox_content_box_wrap').css('width',slidebox_content_box_wrap_size);
	
	// The content movement function
	var slidebox_content_box_count = 1;
	var slidebox_marginleft_new = 0;
	function slidebox_home_move(direction){
		var slidebox_content_box_count_all = $('.slidebox_content_box').size();
		if(direction == 'left'){
			if(slidebox_content_box_count > 1){
				slidebox_marginleft_new = -((slidebox_content_box_count - 2) * (parseInt($('.slidebox_content_box').css('width')) + parseInt($('.slidebox_content_box').css('marginRight'))));
				$('.slidebox_content_box_wrap').animate({'marginLeft': slidebox_marginleft_new + 'px'},400,'swing');
				slidebox_content_box_count --;
			} else {
				slidebox_marginleft_new = -((slidebox_content_box_count_all - 1) * (parseInt($('.slidebox_content_box').css('width')) + parseInt($('.slidebox_content_box').css('marginRight'))));
				$('.slidebox_content_box_wrap').animate({'marginLeft': slidebox_marginleft_new + 'px'},400,'swing');
				slidebox_content_box_count = slidebox_content_box_count_all;
			}
		} else if(direction == 'right'){
			if(slidebox_content_box_count < slidebox_content_box_count_all){
				slidebox_marginleft_new = -(slidebox_content_box_count * (parseInt($('.slidebox_content_box').css('width')) + parseInt($('.slidebox_content_box').css('marginRight'))));
				$('.slidebox_content_box_wrap').animate({'marginLeft': slidebox_marginleft_new + 'px'},400,'swing');
				slidebox_content_box_count ++;
			} else {
				$('.slidebox_content_box_wrap').animate({'marginLeft': '0px'},400,'swing');
				slidebox_content_box_count = 1;
			}
		}
	}
	
	// Initializing the buttons
	$('.slidebox_arrow_left').css('display','block');
	$('.slidebox_arrow_left').click(function(){slidebox_home_move('left'); return false;});
	$('.slidebox_arrow_left').mouseover(function(){$(this).animate({'backgroundPosition': '10px 0px'},150,'swing');});
	$('.slidebox_arrow_left').mouseout(function(){$(this).animate({'backgroundPosition': '20px 0px'},150,'swing');});
	$('.slidebox_arrow_right').css('display','block');
	$('.slidebox_arrow_right').click(function(){slidebox_home_move('right'); return false;});
	$('.slidebox_arrow_right').mouseover(function(){$(this).animate({'backgroundPosition': '10px 0px'},150,'swing');});
	$('.slidebox_arrow_right').mouseout(function(){$(this).animate({'backgroundPosition': '0px 0px'},150,'swing');});
};

function support_init(){
	// Hide all elements
	$('.plusminus').css('backgroundPosition','0px 0px');
	$('.accordion_headline').css('marginBottom','-26px');
	$('.accordion_content').css('display','none');
	
	// Show/Hide content function
	function support_content_toggle(object){
		if($(object).css('marginBottom') != '0px'){
			$(object).children('.plusminus').css('backgroundPosition','0px -14px');
			$(object).css('marginBottom','0px');
			var box_content_new_height = $('.box_content > ' + object).height() + $(object + ' + .accordion_content').height();
			$('.box_content > ' + object).animate({'height': box_content_new_height + 'px'},300,'swing',function(){
				$(object + ' + .accordion_content').fadeIn(500);
				$('.box_content > ' + object).css('height','auto');
			});
		} else {
			var box_content_new_height = $('.box_content > ' + object).height() + $(object + ' + .accordion_content').height() + 97;
			$(object + ' + .accordion_content').fadeOut(500,function(){
				$('.box_content > ' + object).css('height', box_content_new_height + 'px');
				$(object).children('.plusminus').css('backgroundPosition','0px 0px');
				$(object).css('marginBottom','-26px');
				$('.box_content > ' + object).animate({'height': '49px'},300,'swing');
			});
		}
	}
	
	// Initializing the links
	$('a[href*=#]').click(function(){support_content_toggle(this.hash);});
}

/* INIT FUNCTIONS                                                    */
/*********************************************************************/

$(document).ready(function(){
	// Activate Link Rollovers
	link_rollover_init();
	// Activate Window Resizing
	window_resize();
	$(window).resize(function(){window_resize();});
});