/* 
	Title:		Gumtree Global Parameters & Functions
	Copyright:	2008-2009 Gumtree.com
	Date:		3 March 2009
	Authours:	Antonio Lulic/Adam Perfect/Desigan Chinniah
	Email:		anlulic@ebay.com
*/

// Before the DOM has loaded

	// IE6/7 Rounded Corners (Search box, Headings, Main action links, boxouts)
	if ($.browser.msie && $.browser.version < 8) {
		DD_roundies.addRule('div#header', '0 0 5px 5px');
		DD_roundies.addRule('form#search button.submit', '4px');
		DD_roundies.addRule('.section, .column h2', '10px');
		DD_roundies.addRule('div#sidebar div.panel.top','10px 10px 0 0');
		DD_roundies.addRule('div#sidebar div.panel.bottom','0 0 10px 10px');
		DD_roundies.addRule('ul#locale', '12px 0 0 12px');
		DD_roundies.addRule('div#other-gumtrees h3#uk-ireland', '9px 9px 0 0');
		DD_roundies.addRule('.directory', '10px');
		DD_roundies.addRule('.boxout', '10px');
		DD_roundies.addRule('.boxout h3', '9px 9px 0 0');
		DD_roundies.addRule('div.quotes blockquote');

		// Global Nav main actions
		DD_roundies.addRule('div#global-nav #main-actions li.primary, div#global-nav #main-actions li.primary a', '0 12px 12px 0');

		// Homepage Only
		DD_roundies.addRule('#gumtree-forums, div.home #main-actions li.secondary, div.home #main-actions li.secondary a, div.home #main-actions li.primary, div.home #main-actions li.primary a', '12px');
		DD_roundies.addRule('div.home form#search', '4px');
		DD_roundies.addRule('#gumtree-forums h2', '0');
		DD_roundies.addRule('#homepage-featured', '10px');
		DD_roundies.addRule('#homepage-featured h2', '10px 10px 0 0');
	}

// After the DOM has loaded
$(document).ready( function() {

	// IE6 PNG Transparency
	// Commented out, as we've fallen back to a transparent GIF for the logo under IE6.
	// To reenable this, uncomment this section and the <script> tag for DD_belatedPNG in head_default.ttk
/*
	if ($.browser.msie && $.browser.version < 7) {
		$("img[src$='png']").DD_belatedPNG();
		$(this).find('*').each(function(){
			if ($(this).css('background-image').indexOf(".png")!=-1){
				$(this).DD_belatedPNG();
			}
		});
	}
*/

	// Fix buttons with background images in FireFox 2 (by removing the images)
	if ($.browser.mozilla && $.browser.version.substr(0,3)=='1.8') {
		$('ul#main-actions li.secondary a,form#search button.submit').css({'background':'#84c667'});
		$('ul#main-actions li.primary a').css({'background':'#ff8814'});
	}
	
	// Prevent null searches
	$('form#search button.submit').click(function() {
		if ($('form#search input.text').val() == '') {
			alert('Please enter some terms that you\'d like to search for.');
			$('form#search input.text').focus();
			return false;
		}
	});
	
	// Fetch a random tagline keyword - TODO: Move into Perl, not JS
	var taglineWords = [
		'white tuxedos',
		'vintage records',
		'warm weather',
		'pet lizards',
		'free stuff',
		'Spanish lessons',
		'opera singers',
		'Subbuteo sets',
		'Scottish kilts',
		'coffee makers',
		'theatre tickets',
		'free stuff',
		'yellow submarines',
		'cinema posters',
		'Persian cats',
		'porcelain dolls'
	];
    var taglineIndex = Math.floor(Math.random()*taglineWords.length);
    $('#random-tagline-word').text(taglineWords[taglineIndex]);

	// Clear search field on focus, empty on blur
	var keywordsLabel = $('form#search label.example');
	// Show example label on page load if the field is empty
	if ($('form#search input.text').val() == '') {
		keywordsLabel.removeClass('hidden');
	}
	// Clicking the example label focusses on the input
	$('form#search label.example').click( function() {
		$('form#search input.text').focus();
	});
	$('form#search input.text').focus( function() {
		if (!keywordsLabel.hasClass('hidden')) {
			keywordsLabel.addClass('hidden');
		}
	});
	$('form#search input.text').blur( function() {
		if (keywordsLabel.hasClass('hidden') && $(this).val() == '') {
			keywordsLabel.removeClass('hidden');
		}
	});
	
	// Toggle Change City
	$('a.change-city, p.change-city a, p.close-overlay a').click(function(){
		$('a.change-city, p.change-city a').toggleClass('selected');
		$('div#change-city').toggle();
		if ($.browser.msie) {
			if ($.browser.version < 7) {
				$('form#search select').toggle();
			}
			if ($.browser.version < 8) {
				DD_roundies.addRule('#change-city', '12px');
				DD_roundies.addRule('div#change-city h2#change-uk-ireland','10px 10px 0 0');
			}
		}
		return false;
	});
	
	// Scroll Back to Top
	$("a[href='#skip']").click(function(){
		var scrollTime = $(document).height() / 2.5; // This number defines the speed of the scroll; the lower, the slower.
		$.scrollTo('div#skip', scrollTime);
		return false;
	});

	// Make AdSense links open in a new window
	$('ul.adsense_open_new li.adsense_ad a').click(function(){
		window.open(this.href);
		return false;
	});

	// show/hide press coverage sections
	$('#press-coverage h4.group-head a').click(function() {
		var targetEl = $($(this).attr('href'));
		$(this).parent('h4')
			.toggleClass('open')
			.toggleClass('closed');
		targetEl
			.toggleClass('open')
			.toggleClass('closed');
		return false;
	});
	$('#press-releases h4.group-head a').click(function() {
		var targetEl = $($(this).attr('href'));
		$(this).parent('h4')
			.toggleClass('open')
			.toggleClass('closed');
		targetEl
			.toggleClass('open')
			.toggleClass('closed');
		return false;
	});
	// show/hide change city sections
	$('#change-city h2 a').click(function() {
		var targetEl = $($(this).attr('href'));
		$(this).parent('h2')
			.toggleClass('open')
			.toggleClass('closed');
		targetEl
			.toggleClass('show')
			.toggleClass('hidden');
		return false;
	});

/*
	// Randomise Safety Tip Text - TODO Move into Perl, not JS
	var safetyTipTexts = [
		'<strong>Always remember</strong>, Gumtree is a local community so it is wise to be wary of overseas sellers.',
		'<strong>Always remember</strong>, Gumtree is a local community so it is wise to be wary of overseas sellers.',
		'<strong>Always remember</strong>, Gumtree is a local community so it is wise to be wary of overseas sellers.'
	];
	function switchSafetyTip (tipIndex) {
		$('#safety-tips')
			.removeClass('number-1')
			.removeClass('number-2')
			.removeClass('number-3');
		$('#safety-tip-text').html(safetyTipTexts[tipIndex]);
		var newClassName = 'number-'+(tipIndex+1);
		$('#safety-tips').addClass(newClassName);
	}
*/

	// Toggle Homepage 'More Categories'
/*
	$('p.show-links.more-links a, p.show-links.fewer-links a').click(function(){
		$('li.' + $(this).attr('rel')).toggle();
		$(this).parent()
			.toggleClass('more-links')
			.toggleClass('fewer-links');
		if ($(this).children('span').text() == 'More') {
			if ($(this).hasClass('singular')) {
				$(this).children('span').text('Less')
			} else {
				$(this).children('span').text('Fewer')
			}
		} else {
			$(this).children('span').text('More')
		};
		return false;
	});
*/

});
	
function open_close(el) {
	if (el.hasClass('open')) {
		el.removeClass('open');
		el.addClass('closed');
	}
	else {
		el.removeClass('closed');
		el.addClass('open');
	}
}

// Activate ad placements and alter related elements
function activateAdPlacements() {
	$('div.ad').each(function(){
		var adId = $(this).parent().eq(0).attr('id');
		if ($(this).children('iframe.gumtree-dart-wrapper').contents().find('body').children('div.no-banner').length == 0 || $(this).children('iframe[id^="google_ads_frame"]').length == 0 ) {
			$(this).addClass('show');
			$('.' + adId + '-related').addClass('has-placement');
		}
	});
}

window.onload = activateAdPlacements;