$.ajaxSetup({
  url: 'http://www.informatie-site.nl/okeb/ajaxhandler.php',
	type: 'post'
});
$(document).ready(function() {
	$("h2.my_environment_orange").click(function() {
		ToggleContainer($(this).attr('open_id'));
	});
	// Image scalling...
	if ($.browser.msie) {
		$("img.aanbieding_detail_image").scaleImage({scale:"fit"});
		$("img.spotlight_image").scaleImage({scale:"fit"});
		$("img.frontpage_block_image").scaleImage({scale:"fit"});
		$("img.webshop_record_image").scaleImage({scale:"fit"});
		$("img.last_viewed_image").scaleImage({scale:"fit",center:false});
	}
	$("img.frontpage_slideshow_image").scaleImage({scale:"fit",center:false});
	$('#frontpage_spotlight_1').cycle({ fx:  'fade'	});
	$('#frontpage_spotlight_2').cycle({	fx: 'fade' });
	$('#frontpage_spotlight_3').cycle({ fx:  'fade'	});
	$('#frontpage_spotlight_4').cycle({ fx:  'fade'	});
	$('#frontpage_spotlight_1').cycle('pause');
	$('#frontpage_spotlight_2').cycle('pause');
	$('#frontpage_spotlight_3').cycle('pause');
	$('#frontpage_spotlight_4').cycle('pause');
	$('#frontpage_spotlight_1').show();
	$('#frontpage_spotlight_2').show();
	$('#frontpage_spotlight_3').show();
	$('#frontpage_spotlight_4').show();
	StartupOne();
	$('#frontpage_spotlight_2').show();
	setTimeout('StartupTwo()',1500);
	setTimeout('StartupThree()',3000);
	setTimeout('StartupFour()',4500);
	SetMyEnvironmentButtons();
	$('.my_environment_record').live('mouseover', function() { ShowIcons(this) });
	$('.my_environment_record').live('mouseout', function() { HideIcons(this) });
	$('.my_brand_record').live('mouseover', function() { ShowIcons(this) });
	$('.my_brand_record').live('mouseout', function() { HideIcons(this) });
	$('.my_search_record').live('mouseover', function() { ShowIcons(this) });
	$('.my_search_record').live('mouseout', function() { HideIcons(this) });
	$('.last_viewed_record').live('mouseover', function() { ShowIcons(this) });
	$('.last_viewed_record').live('mouseout', function() { HideIcons(this) });
	$('.my_environment_save').live('click', function() { SaveItem(this) });
	$('#offer_save').live('click', function() { SaveItem(this) });
	$('.offer_save').live('click', function() { SaveItem(this) });
	$('.my_environment_delete').live('click', function() { DeleteItem(this) });
	PositionBalloon();
	setTimeout('FadeBalloon()',10000);
});
$(window).bind('load', function() {
	CheckImages();																
});

function ToggleContainer(ths) {
	$('#'+ths).slideToggle('fast', function() {
		$.ajax({ data: "task=set_my_environment_state&div="+ths+"&visible="+$('#'+ths).is(":visible")+"&user_id="+$('#user_id').val() });
		// Make sure the buttons are correctly positioned
		SetMyEnvironmentButtons();
  });
}

function CheckImages() {
	blnAlert = true;
	$('img').each(function() {
		if((typeof this.naturalWidth != "undefined" && this.naturalWidth == 0 ) || this.readyState == 'uninitialized' ) {
			// Image broken or not found, replace with missing
			$(this).attr('src', '/okeb/styles/images/missing.jpg');
		}
	});
}
function StartSlide(no) {
	$('#frontpage_spotlight_'+no).cycle('resume');
}
function StartupOne() {
	$('#frontpage_spotlight_1').cycle({ fx:  'fade', timeout:8000	});
}
function StartupTwo() {
	$('#frontpage_spotlight_2').cycle({ fx:  'fade', timeout:8000	});	
}
function StartupThree() {
	$('#frontpage_spotlight_3').cycle({ fx:  'fade', timeout:8000	});	
}
function StartupFour() {
	$('#frontpage_spotlight_4').cycle({ fx:  'fade', timeout:8000	});	
}
function SetMyEnvironmentButtons() {
	// Position the buttons relative to the parent-div
	$('.my_environment_save').each(function (i) { 
		// Get the parent
		var parent = $(this).parent();
		// Get the location and width
		position = $(parent).position();
		positionX = position.left;
		positionY = position.top + 3;
		// Get the button
		saveButton = parent.children('.my_environment_save');
		$(saveButton).css('left',positionX+"px");
		$(saveButton).css('top',positionY+"px");
	});
	// Position the buttons relative to the parent-div
	$('.my_environment_delete').each(function (i) { 
		// Get the parent
		var parent = $(this).parent();
		// Get the location and width
		position = $(parent).position();
		positionX = position.left + $(parent).width() - 8;
		positionY = position.top + 3;
		// Get the button
		delButton = parent.children('.my_environment_delete');
		$(delButton).css('left',positionX+"px");
		$(delButton).css('top',positionY+"px");
	});
}
function ShowIcons(ths) {
	$(ths).children('.my_environment_save').show();
	$(ths).children('.my_environment_delete').show();
}
function HideIcons(ths) {
	$(ths).children('.my_environment_save').hide();
	$(ths).children('.my_environment_delete').hide();
}
function SaveItem(ths) {
	var saveType = $(ths).attr('type');
	var saveID = $(ths).attr('save_id');
	var userID = $('#user_id').val();
	if (saveType != 'search') {
		$.ajax({ data: "task=save_item&tp="+saveType+"&id="+saveID+"&uid="+userID, success: function(data) { UpdateSavedItem(saveType,data); } });
	} else {
		var query = $('#query').val();
		var query_type = $('#query_type').val();
		var query_location = $('#query_location').val();
		var query_description = $('#query_description').val();
		$.ajax({ data: "task=save_item&tp="+saveType+"&uid="+userID+"&q="+query+"&qt="+query_type+"&ql="+query_location+"&qd="+query_description, success: function(data) { UpdateSavedItem(saveType,data); } });
	}
}
function UpdateSavedItem(tp,data) {
	if (data != '') {
		switch(tp) {
			case 'aanbiedingen':
				if ($('#mijn_aanbiedingen_container').html() == '<p>U heeft nog geen aanbiedingen opgeslagen...</p>') {
					$('#mijn_aanbiedingen_container').html('');
				}
				// Add the newly saved product
				$('#mijn_aanbiedingen_container').prepend(data);
				$('#aanbiedingen_count').html(parseInt($('#aanbiedingen_count').html())+1);
				$('#aanbiedingen_counter').html(parseInt($('#aanbiedingen_counter').html())+1);
				if ( !$('#mijn_aanbiedingen_container').is(':visible')) { ToggleContainer('mijn_aanbiedingen_container') }
				ShowMessage('Item opgeslagen.',2000);
				break;
			case 'webshops':
				$('#webshops_count').html(parseInt($('#webshops_count').html())+1);
				ShowMessage('Item opgeslagen.',2000);
				break;
			case 'brands':
				$('#brands_count').html(parseInt($('#brands_count').html())+1);
				ShowMessage('Item opgeslagen.',2000);
				break;
			case 'search':
				ShowMessage('Zoek-actie opgeslagen.',2000);
				break;
		}
	} else {
		if (tp != 'search') {
			ShowMessage('Dit item heeft u al eerder opgeslagen.',2000);
		} else {
			ShowMessage('Deze zoek-actie heeft u al eerder opgeslagen.',2000);
		}
	}
	// Make sure the buttons are correctly positioned
	SetMyEnvironmentButtons();
}
function DeleteItem(ths) {
	var delType = $(ths).attr('type');
	var delID = $(ths).attr('delete_id');
	var userID = $('#user_id').val();
	$(ths).parent().slideToggle('slow', function() {
		$(ths).parent().remove();
		// Make sure the buttons are correctly positioned
		SetMyEnvironmentButtons();
		// Actually delete the item through AJAX
		$.ajax({ data: "task=delete_item&tp="+delType+"&id="+delID+"&uid="+userID });
		// Set the counter
		$('#'+delType+'_count').html(parseInt($('#'+delType+'_count').html())-1);
		$('#'+delType+'_counter').html(parseInt($('#'+delType+'_counter').html())-1);
  });
}
function ShowMessage(msg,duration) {
	// Set the message
	$('#message').html(msg);
	// Position it
	var x = Math.round(($(window).width() - $('#message').outerWidth()) / 2);
	var y = 16; //$(window).scrollTop()+16;
	$('#message').css('left',x+'px');
	$('#message').css('top',y+'px');
	// Show it
	$('#message').fadeIn('fast');
	// And close it
	setTimeout("$('#message').fadeOut('fast')",duration);
}
function OpenCreateNewsletter() {
	// Close mijn_nieuwsbrieven_container and open the create_newsletter
	$('#mijn_nieuwsbrieven_container').slideToggle('fast', function() {
		$('#create_newsletter').slideToggle('fast');
	});
}

// NEWSLETTER CREATION
function SaveNewsletter() {
	CancelNewsletter();
}
function CancelNewsletter() {
	// Close create_newsletter and open the mijn_nieuwsbrieven_container
	$('#create_newsletter').slideToggle('fast', function() {
		$('#mijn_nieuwsbrieven_container').slideToggle('fast');
	});	
}
function ChangeNewsletterFrequency() {
	var freq = $('#newsletter_frequency').val();
	if (freq != 'daily' && freq != 'every-other-day') {
		$('#send_day').show();
	} else {
		$('#send_day').hide();		
	}
}
function SendNewsletterExample() {
	// Collect the necessary information and send it through AJAX
	var userID = $('#user_id').val();
	var nlname = $('#nl_name').val();
	var nloffers = $('#nl_offers').is(':checked');
	var nloffercategories = $('#nl_offer_categories').val();
	var nloffershops = $('#nl_offer_webshops').val();
	var nlofferbrands = $('#nl_offer_webshops').val();
	var nloffertype = ($('#include_offers_type_all').is(':checked')) ? $('#include_offers_type_all').val() : $('#include_offers_type_latest').val();
	var nldiscounts = $('#nl_discounts').is(':checked');
	var nldiscountcategories = $('#nl_discount_categories').val();
	var nldiscountshops = $('#nl_discount_webshops').val();
	var nldiscountbrands = $('#nl_discount_webshops').val();
	var nldiscounttype = ($('#include_discounts_type_all').is(':checked')) ? $('#include_discounts_type_all').val() : $('#include_discounts_type_latest').val();
	var nlwebshops = $('#nl_webshops').is(':checked');
	var nlwebshopcategories = $('#nl_webshop_categories').val();
	var nlwebshopshops = $('#nl_webshop_webshops').val();
	var nlwebshopbrands = $('#nl_webshop_brands').val();
	var nlwebshoptype = ($('#include_webshops_type_all').is(':checked')) ? $('#include_webshops_type_all').val() : $('#include_webshops_type_latest').val();
	var nlbrands = $('#nl_brands').is(':checked');
	var nlbrandcategories = $('#nl_brand_categories').val();
	var nlbrandshops = $('#nl_brand_webshops').val();
	var nlbrandbrands = $('#nl_brand_brands').val();
	var nlbrandtype = ($('#include_brands_type_all').is(':checked')) ? $('#include_brands_type_all').val() : $('#include_brands_type_latest').val();
	var xmldata = "task=send_newsletter_preview&uid="+userID+
								 "&nlname="+nlname+
								 "&nloffers="+nloffers+
								 "&nloffercategories="+nloffercategories+
								 "&nloffershops="+nloffershops+
								 "&nlofferbrands="+nlofferbrands+
								 "&nloffertype="+nloffertype+
								 "&nldiscounts="+nldiscounts+
								 "&nldiscountcategories="+nldiscountcategories+
								 "&nldiscountshops="+nldiscountshops+
								 "&nldiscountbrands="+nldiscountbrands+
								 "&nldiscounttype="+nldiscounttype+
								 "&nlwebshops="+nlwebshops+
								 "&nlwebshopcategories="+nlwebshopcategories+
								 "&nlwebshopshops="+nlwebshopshops+
								 "&nlwebshopbrands="+nlwebshopbrands+
								 "&nlwebshoptype="+nlwebshoptype+
								 "&nlbrands="+nlbrands+
								 "&nlbrandcategories="+nlbrandcategories+
								 "&nlbrandshops="+nlbrandshops+
								 "&nlbrandbrands="+nlbrandbrands+
								 "&nlbrandtype="+nlbrandtype;
	if (nlname == '') {
		ShowMessage("U heeft de nieuwsbrief nog geen naam gegeven. Het voorbeeld zal zonder naam worden verstuurd.",2500);
		setTimeout("AjaxPreview('"+xmldata+"')",2500);
	} else {
		AjaxPreview(xmldata);
	}
}
function AjaxPreview(xmldata) {
	$.ajax({
		data: xmldata,
		success: function(data) {
			ShowMessage(data,2500);
		}
	});
}
function PositionBalloon() {
	$('#social_media_balloon').hide();
	// Get the position of the header
	var pos = $('#header').position();
	left = Math.round(pos.left);
	$('#social_media_balloon').css('left',left+480+"px");
	$('#social_media_balloon').show();
}
function FadeBalloon() {
	$('#social_media_balloon').fadeOut();
}
