$(function() {
	// fancybox trigger
	$('.fancybox').fancybox();
	$('.fancyiframe').fancybox({
		type: 'iframe',
		width: 960,
		height: '95%'
	});
	$('#hnappur').fancybox({
			'hideOnOverlayClick'	: false,
			'showCloseButton'		: false,
			'enableEscapeButton'	: false
	});
	$('.fancy_img').fancybox({
        'transitionIn'  :       'elastic',
        'transitionOut' :       'elastic',
        'speedIn'               :       600,
        'speedOut'              :       200,
        'overlayShow'   :       false
    });
    // banner control
    $('#min_banner').click(function() {
    	var stat = $(this).attr('rel').split(';');
    	if (stat[0] == 'large') {
    		$.get('/ajax/', {banner_sub_size: 'small', at_site: stat[1]});
    		$('#banner_sub').animate({
			    height: '50px'
			  }, 1000);
    		$(this).attr('rel', 'small');
    		$(this).removeClass('make_small');
    		$(this).addClass('make_large');
    	}
    	else {
    		$.get('/ajax/', {banner_sub_size: 'large', at_site: stat[1]});
    		$('#banner_sub').animate({
			    height: '277px'
			  }, 1000);
    		$(this).attr('rel', 'large');
    		$(this).removeClass('make_large');
    		$(this).addClass('make_small');
    	}
    });
    // scroll of prod
    $('.make_scroll').jScrollPane({
		enableKeyboardNavigation	: false
	});
    // menu drop down
	$('#menu > li').bind('mouseover', jsddm_open);
	$('#menu > li').bind('mouseout',  jsddm_timer);
	
	// login
	$('#login').toggle(function() {
		$('#login_box').slideDown('fast');
		$(this).css('background', '#fff');
		$('#navision_username').focus();
	}, function() {
		$('#login_box').slideUp('fast');
		$(this).css('background', 'none');
	});
	$('#nav_log_form').submit(function() {
		var nav_user = $('#navision_username').val();
		var nav_pass = $('#navision_password').val();
		
		ajax_loader('.check_login', 'center');
		
		$.post('/ajax/?nav_login=TRUE', {
			navision_username	: nav_user,
			navision_password	: nav_pass
		}, function(data) {
			if (data.callback && data.callback.status) {
				eval(data.callback.javascript);
			}
			else {
				$('.check_login').html(data.callback.message);
			}
		}, 'json');

		return false;
	});
	// tooltip
	set_tooltip();
	// reflection control
	reflect();
	// search button
	$('.search_button').toggle(function() {
		$('.search_frame').fadeTo('slow', 1);
		$('#search_input').show();
		$(this).css('background-position','-1154px -47px');
	},function() {
		$('.search_frame').fadeTo('slow', 0);
		$('#search_input').hide();
		$(this).css('background-position','-1110px -47px');
	});
	$('#search_input').keyup(function(event){
	    if (event.keyCode == 13) {
	    	var search = $('#search_input').val();
	        window.location = "/?search="+escape(search);
	    }
	});
	// prod menu
	$('a.linker:not(.selected)').hover(function() {
		//$(this).siblings('.menu_image').css('filter', 'alpha(opacity=100)');
		$(this).siblings('.menu_image_holder').find('.menu_image').slideDown('fast');
		$(this).siblings('span').animate({top: -122}, 200);
	}, function() {
		//$(this).siblings('.menu_image').fadeTo('fast', 0);
		$(this).siblings('.menu_image_holder').find('.menu_image').slideUp('fast');
		$(this).siblings('span').animate({top: -142}, 200);
	});
	
	// hover_fade
	$('.bottom_ads>a').hover(function() {
		$(this).fadeTo('500', .75);
	}, function() {
		$(this).fadeTo('800', 1);
	});

	// spec accordion
	$('.prod_spec_info_button').live('mouseover mouseout', function(event) {
	  	if (event.type == 'mouseover') {
	   		$(this).addClass('spec_i_hover');
	   		$(this).children('div').removeClass('spec_i_norm_end');
			$(this).children('div').addClass('spec_i_hover_end');
	  	} else {
	    	$(this).removeClass('spec_i_hover');
	    	$(this).children('div').removeClass('spec_i_hover_end');
			$(this).children('div').addClass('spec_i_norm_end');
	  	}
	});
	$('.prod_spec_info_button').live('click', function() {
		if($(this).hasClass('spec_i_sel')) {
			$(this).removeClass('spec_i_sel');
			$(this).addClass('spec_i_norm');
			$(this).children('div').removeClass('spec_i_sel_end');
			$(this).children('div').addClass('spec_i_norm_end');
			$(this).next('.spec_list').slideUp('fast');
		}
		else {
			$(this).removeClass('spec_i_hover');
			$(this).addClass('spec_i_sel');
			$(this).children('div').removeClass('spec_i_hover_end');
			$(this).children('div').addClass('spec_i_sel_end');
			$(this).next('.spec_list').slideDown('fast');
		}
	});
	$('#open_controller').live('mouseover mouseout', function(event) {
	  	if (event.type == 'mouseover') {
	  		if($(this).children('span').html() == 'Opna alla') {
		   		$(this).removeClass('open_contr_open');
				$(this).addClass('open_contr_hover');
				$(this).children('span').css('color', '#fff');
			}
	  	} else {
	  		if($(this).children('span').html() == 'Opna alla') {
	    		$(this).removeClass('open_contr_hover');
				$(this).addClass('open_contr_open');
				$(this).children('span').css('color', '#000');
			}
	  	}
	});
	$('#open_controller').live('click', function() {
		if($(this).children('span').html() == 'Opna alla') {
			$(this).removeClass('open_contr_hover');
			$(this).addClass('open_contr_close');
			$(this).children('span').css('color', '#fff');
			$('.prod_spec_info_button').removeClass('spec_i_norm');
			$('.prod_spec_info_button').addClass('spec_i_sel');
			$('.prod_spec_info_button').children('div').removeClass('spec_i_norm_end');
			$('.prod_spec_info_button').children('div').addClass('spec_i_sel_end');
			$('.spec_list').slideDown('fast');
			$(this).children('span').html('Loka öllum');
		}
		else {
			$(this).removeClass('open_contr_close');
			$(this).addClass('open_contr_open');
			$(this).children('span').css('color', '#000');
			$('.prod_spec_info_button').removeClass('spec_i_sel');
			$('.prod_spec_info_button').addClass('spec_i_norm');
			$('.prod_spec_info_button').children('div').removeClass('spec_i_sel_end');
			$('.prod_spec_info_button').children('div').addClass('spec_i_norm_end');
			$('.spec_list').slideUp('fast');
			$(this).children('span').html('Opna alla');
		}
	});
	
	// tab products functionality
	$('.tab').live('click', function() {
		ajax_loader('.shop_content_main', 'center', 120);
		var where_to = $(this).attr('rel');
		var this_one = $(this);
		var template = '';
		if ($(this).hasClass('template_view_product')) {
			template = '?template=view_product';
		}
		$.post('/ajax/' + template,
			{
				change_tab	: where_to,
				data		: $('#tab_json_data').html()
			},
			function(data) {
				$('.shop_content_main').css('background', 'none');
				$('.shop_content_main').html(data);
				$('.tab').removeClass('tab_sel');
				$('.tab').addClass('tab_norm');
				this_one.removeClass('tab_norm');
				this_one.addClass('tab_sel');
				if (where_to == 'info' || where_to == 'spec') {
					$('.colors').show();
				}
				else {
					$('.colors').hide();
				}
			}
		);
	});
	$('.tab_norm').live('hover', function() {
		$(this).children('span').css('color', '#000');
	}, function() {
		$(this).children('span').css('color', '#434343');
	});
	
	$('.minimize_prod_compare_disp').toggle(function() {
		if (fast_comp == 1) {
			$(this).parent().prev().hide();
		}
		else {
			$(this).parent().prev().slideUp('slow');
		}
		fast_comp = 0;
		$(this).animate({'rotate':180},500);

		return false;
	}, function() {
		$(this).parent().prev().slideDown('slow');
		$(this).animate({'rotate':0},500);

		return false;
	});
	
	$('.minimize_prod_disp').toggle(function() {
		if (faster == 1) {
			$(this).parent().prev().prev().hide();
			$(this).parent().prev().hide();
		}
		else {
			$(this).parent().prev().prev().slideUp('slow');
			$(this).parent().prev().slideUp('slow');
		}
		faster = 0;
		$(this).animate({'rotate':180},500);
		var options = {path: '/', expires: 86400};
		$.cookie('filter', 'hidden', options);
		return false;
	}, function() {
		$(this).parent().prev().prev().slideDown('slow');
		$(this).parent().prev().slideDown('slow');
		$(this).animate({'rotate':0},500);
		var options = {path: '/', expires: 86400};
		$.cookie('filter', 'visible', options);
		return false;
	});
	
	// compare functionality
	$('.compare').live('click', function() {
		var info = $(this).attr('rel').split(';');
		
		$.get('/ajax/', {compare: true, web_category_id: escape(info[0]), item_id: escape(info[1]), web_category_sub_no: escape(info[2]), name: escape(info[3]), image: escape(info[4])}, function(data) {
			if (data.success) {
				var slot_id = eval(data.javascript);
				$(slot_id).attr('title', info[3]);
				$(slot_id).removeClass('slot');
				$(slot_id).addClass('slot_use');
				$(slot_id).html('<img src="/navision_image.php?filename=' + escape(info[4]) + '&size=48x38" border="0" /><div class="remove_compare"></div>');
				set_tooltip();
			}
			else {
				alert(data.message);
			}
		}, 'json');

		var prod_disp = $('.minimize_prod_compare_disp');
		if (prod_disp.parent().prev().is(':hidden')) {
			prod_disp.trigger('click');
		}

		return false;
	});
	$('.slot_use>div.remove_compare').live('mouseover mouseout', function(event) {
	  	if (event.type == 'mouseover') {
	  		$(this).css({backgroundPosition: '9px 5px'});
	  	} else {
	  		$(this).css({backgroundPosition: '9px -35px'});

	  	}
	});
	$('.slot_use>div.remove_compare').live('click', function(event) {
		var slot_id = $(this).parent().attr('id');
		var web_category_sub_no = $(this).parent().attr('rel');
		var this_slot = $(this).parent();
	  	$.get('/ajax/', {remove_slot: slot_id, web_category_sub_no: web_category_sub_no}, function(data) {
	  		eval(data);
	  		set_tooltip();
	  	});
	});
	
	$('a.facebook').click(function() {
		var url = $(this).attr('href');
		window.open(url, 'sharer', 'toolbar=0,status=0,width=626,height=436');
		
		return false;
	});
	
	$('a.twitter').click(function() {
		var url = $(this).attr('href');
		window.open(url);
		
		return false;
	});
	// staff
	$('.emp_line').live('click', function() {
		var open_me = '#detail_'+$(this).attr('rel');
		var close_me = '#short_'+$(this).attr('rel');
		$(close_me).hide('fast');
		$(open_me).show('fast');
	});
	$('.emp_close').live('click', function() {
		var open_me = '#short_'+$(this).attr('rel');
		var close_me = '#detail_'+$(this).attr('rel');
		$(close_me).hide('fast');
		$(open_me).show('fast');
	});
	$('.board_more>a').live('click', function() {
		var board_id = $(this).attr('rel');
		$.get('/ajax/', {change_board: board_id}, function(data) {
			$('.board_box_large').html(data);
		});
		return false;
	});
	
	$('.staff_button>a').click(function() {
		var rel_info = $(this).attr('rel');
		
		if (rel_info == 'staff') {
			$('.staff_button').each(function() {
				$(this).children('a').css({'color':'#4a515a'});
			})
			$(this).css({'color':'white'});
			$('#emp_lineup').show();
			$('#board_lineup').hide();
			$('#man_lineup').hide();
		}
		if (rel_info == 'managers') {
			var board_id = $('.managers:first>div.board_more>a').attr('rel');
			//alert(board_id);
			$.get('/ajax/', {change_board: board_id}, function(data) {
				$('.board_box_large').html(data);
			});
			$('.staff_button').each(function() {
				$(this).children('a').css({'color':'#4a515a'});
			})
			$(this).css({'color':'white'});
			$('#emp_lineup').hide();
			$('#board_lineup').hide();
			$('#man_lineup').show();
		}
		if (rel_info == 'board') {
			var board_id = $('.board:first>div.board_more>a').attr('rel');
			//alert(board_id);
			$.get('/ajax/', {change_board: board_id}, function(data) {
				$('.board_box_large').html(data);
			});
			$('.staff_button').each(function() {
				$(this).children('a').css({'color':'#4a515a'});
			})
			$(this).css({'color':'white'});
			$('#emp_lineup').hide();
			$('#board_lineup').show();
			$('#man_lineup').hide();
		}

		$('.staff_button').each(function() {
			$(this).removeClass('staff_sel');
		});

		$(this).parent().addClass('staff_sel');
		return false;
	});

	$('.image_slider').live('click', function() {
		var image_src = $('img', $(this)).attr('src').replace(/size\=(?:\d+)x?(?:\d*?)$/ig, 'size=800x600');
		$('img#image_display').attr('src', image_src);

		return false;
	});

	$('.color').live('click', function() {
		if ($(this).attr('rel')) {
			$('#main_image').attr('src', $(this).attr('rel'));
		}

		return false;
	});
	// read side news
	$('.side_read_news').toggle(function() {
		$(this).html('Minnka fréttaboxið');
		$(this).prev('.side_box_news').css('height','auto');
		return false;
	}, function() {
		$(this).html('Lesa alla fréttina');
		$(this).prev('.side_box_news').animate({
			height: '60px'
		}, 500);
		return false;
	});

	$('#disclaimer').live('click', function(e) {
		if ($('#disclaimer_full').hasClass('closed')) {
			$('#disclaimer_full').slideDown();
			$('#disclaimer_full').removeClass('closed');
		}
		else {
			$('#disclaimer_full').slideUp();
			$('#disclaimer_full').addClass('closed');
		}

		e.preventDefault();
	});

	$('.tiptip_activate').live('click', function() {
		set_tooltip();
	});
	
	//Rollover virkni fyrir birgja takka
	
	$('.brandcategory').hover(function() {
		var info = $(this).attr('rel').split(',', 2);
		if(info[1] == 'no') {
			$(this).find('.first').css({'background':'url(/sysimages/sprite.png) no-repeat 0px -441px', 'color':'#fff'});
			$(this).find('.end').css({'background':'#fff url(/sysimages/sprite.png) no-repeat -245px -441px'});
		}
	}, function() {
		var info = $(this).attr('rel').split(',',2);
		if(info[1] == 'no') {
			$(this).find('.first').css({'background':'url(/sysimages/sprite.png) no-repeat 0px -462px', 'color':'#686b6e'});
			$(this).find('.end').css({'background':'#fff url(/sysimages/sprite.png) no-repeat -245px -462px'});
		}
	});
	
	$("#brandsearch").keypress(function(event)
	{
		if (event.keyCode == '13') 
		{
			loadBrandsByTerm();
		}
	});
	
	$('.brandRow').live('click',function(event)
	{
		if(event == 'click')
		{
			alert("ha");
		}
	});
	
	/*$('.tooltipar').tinyTips('dark', 'title');
	$('.tiptip_activate').click(function() {
		$(window).unbind('.tooltipar');
		$('.tooltipar').removeData('tinyTip');
		add_tinyTip();
		//alert('ehhh');
	});*/
	
});

function openBrand(id)
{
	$.fancybox({
		href : '/brand.php?id='+id,
		type: 'iframe',
		width: 800,
		height: 600
	});
}

function minimize_prod_disp_hide() {
	$('.minimize_prod_disp').click();
}

function get_position(for_me) {
	var win_width = $(window).width();
	var my_width = $(for_me).width();
	
	var pos = (win_width - my_width) / 2;
	
	$(for_me).css({'right':pos+'px'});
}
function get_pos_right(for_me) {
	var win_width = $(window).width();
	var other_width = 954;
	
	var pos = (win_width - other_width) / 2;
	
	$(for_me).css({'right':pos+'px'});
}
function check_blog_height() 
{
	/*$('.blog_item>a.title').click(function() 
	{
		if ($(this).next().next().next().children('.blog_view_more').html() == 'Sjá meira') 
		{
			$(this).next().next().next().children('.content_holder').slideDown('slow');
			$(this).next().next().next().children('.blog_view_more').html('Sjá minna');
		}else 
		{
			$(this).next().next().next().children('.content_holder').slideUp('slow');
			$(this).next().next().next().children('.blog_view_more').html('Sjá meira');
		}
		return false;
	});*/
}
function set_tooltip() {
	$('.tooltip').tipTip({
		defaultPosition	: 'top',
		maxWidth		: '400px'
	});
}
/*function add_tinyTip() {
	$('.tooltipar').tinyTips('dark', 'title');
}*/
function add_this() {
	var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
	if (window.addthis){
	    window.addthis = null;
	}
	$.getScript( script );
}

/* Made by Mathias Bynens <http://mathiasbynens.be/> */
function number_format(a, b, c, d) {
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if (!f[0]) {
		f[0] = '0';
	}
	if (!f[1]) {
		f[1] = '';
	}
	if (f[1].length < b) {
		g = f[1];
		for (i=f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j+=3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '' : c;
	return f[0] + c + f[1];
}

// menu drop down
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() {
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function jsddm_close() {
	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer() {
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function ajax_loader(elem, align, padding) {
	if (!align) {
		align = 'left';
	}
	if (!padding) {
		padding = 2;
	}
	$(elem).html('<div style="text-align: ' + align + '; padding-top: ' + padding + 'px;"><img src="/sysimages/ajax-loader.gif" alt="Augnablik..." /><br />Augnablik...</div>');
	return true;
}

function reflect() {
	var speOpt = $.extend({
		height: 30,
		opacity: 0.2
	}, speOpt);
	$('.reflect_me').reflect(speOpt);

	return true;
}

function slider_init(slider_options, callback) {
	$("#slider-range").slider({
		animate: true,
		range: true,
		min: slider_options.price_min_init,
		max: slider_options.price_max_init,
		values: [slider_options.price_min_value, slider_options.price_max_value],
		step: slider_options.step,
		slide: function(event, ui) {
			$("#amount").val(number_format(ui.values[0], 0, ',', '.') + ' kr. - ' + number_format(ui.values[1], 0, ',', '.') + ' kr.');
		},
		stop: function(event, ui) {
			slider_options.price_min_value = price_min = ui.values[0];
			slider_options.price_max_value = price_max = ui.values[1];

			if (callback) {
				callback();
			}
		}
	});
	$("#amount").val(number_format($("#slider-range").slider("values", 0), 0, ',', '.') + ' kr. - ' + number_format($("#slider-range").slider("values", 1), 0, ',', '.') + ' kr.');

	return slider_options;
}

function loadBrands(id,limit)
{
	$.get("/ajax/",{brands:true,catid:id,li:limit},function(data)
	{
		$('#BrandsContent').fadeOut('fast',function(){
			$('#BrandsContent').html(data);
			$('#BrandsContent').fadeIn('fast');
			$('.brand_button').removeClass('brand_selected');
			$("#brandbutton_"+id).addClass('brand_selected');
			$("#brandscategory").val(id);
		});
	});
}

function loadBrandsByTerm()
{
	var term = $("#brandsearch").val();
	var category = $("#brandscategory").val();
	$.get("/ajax/",{brandsearch:term,cat:category},function(data)
	{
		$('#BrandsContent').fadeOut('fast',function()
		{
			$('#BrandsContent').html(data);
			$('#BrandsContent').fadeIn('fast');
		});
	});	
}

document.onclick = jsddm_close;

