jQuery.fn.exists = function(){ return this.length > 0;};
var STREET_VIEW = "http://maps.googleapis.com/maps/api/streetview?size=300x300&sensor=false&location=";
var MAPS = "http://maps.google.com/maps/api/staticmap?size=300x300&sensor=false&markers=color:blue|label:S|";
var INSTRUCT = "Select A Retailer Above To View Its Location:<br>Currently Viewing ";

$(document).ready(function() {	
	$('a.competition').colorbox({ inline : true, width: '800px', opacity : 0.3 });	
	if ($('div.primary_content').exists()) indexPageEvents();	
	if ($('td.felsBridal').exists()) upperNthEvents();	
	if ($('td.ccWard').exists()) lowerNthEvents();	
	if ($('td.davisBrid').exists()) sthEvents();
	if ($('h1.gallery_title').exists()) galleryEvents();
	if ($('a.img_group').exists()) viewGownEvents();
    if ($('h1.gallery_title').exists()) {
	$('div.page_body').height("1500px");
	$('div.navigation').height("1470px");
    }
});

function viewGownEvents() {
	$('a.img_group').colorbox({ rel : 'img_group', opacity : 0.3, current : '', maxHeight : '600px'});
}

$(window).load(function() {
	if ($('#slider').exists()) 
        $('#slider').nivoSlider({ effect : 'boxRandom,fade,boxRain,boxRainGrow' });
});

function galleryEvents() {
	$('table.gallery_grid td').hover(function() {
		$(this).find('div.gallery_caption').fadeToggle('slow');
	});
}

function indexPageEvents() {
	$('div.photo_one').innerfade({
		speed : 'slow',
		timeout : 3000,
		containerheight : '250px'
	});
	$('div.photo_two').innerfade({
		speed : 'slow',
		timeout : 4000,
		containerheight : '250px'
	});
	$('div.photo_three').innerfade({
		speed : 'slow',
		timeout : 5000,
		containerheight : '250px'
	});
}

function showLocation(place, address, fov) {
	var field = (fov) ? "&fov=50" : "";
	$('p.location_line').html(INSTRUCT + place);
	$('img.street_canvas').attr({ 'src' : STREET_VIEW + address + field });
	$('img.map_canvas').attr({ 'src' : MAPS + address });
}

function sthEvents() {
	$('td.davisBrid').click(function() { 
		showLocation("Davis Bridal Salon", "237+Brougham+Street,+Christchurch", false); 
	});
	$('td.houseKavina').click(function() { 
	    $('p.location_line').html(INSTRUCT + "House of Kavina");
	    $('img.map_canvas').attr({ 'src' : MAPS + "335+South+Road,+Caversham,+Dunedin" });
	    $('img.street_canvas').attr({ 'src' : '../images/kavina.jpg' });
	});
	$('td.4everAfter').click(function() { 
		showLocation("4Ever After Bridal", "80+Salford+Street,+Invercargill", false); 
	});
}

function lowerNthEvents() {
	$('td.ccWard').click(function() { 
		showLocation("CC Ward Ltd", "6+Devon+Street+East,+New+Plymouth", true); 
	});		
	$('td.bridalStud').click(function() { 
		showLocation("Bridal Studio", "19+Ashley+Street,+Palmerston+North", true); 
	});		
	$('td.gownsOnG').click(function() {
		showLocation("Gowns on Gloucester", "276+Gloucester+Street,+Taradale,+Napier", true);
	});		
	$('td.bridesOnT').click(function() {
		showLocation("Brides On Thorndon", "9+Brandon+Street,+Wellington", false);
	});		
	$('td.weddBelles').click(function() {
		showLocation("Wedding Belles", "415+Stokes+Valley+Rd,+Stokes+Valley,+Lower+Hutt", false);
	});
}

function upperNthEvents() {
	$('td.felsBridal').click(function() {
		showLocation("Felicity's Bridal", "92+Albert+Street,+Auckland", true);
	});
	$('td.jensBridal').click(function() {
		showLocation("Jenny's Bridal of Newmarket", "383+Khyber+Pass+Road,+Newmarket,+Auckland", true);
	});
	$('td.chcsforbrkfst').click(function() {
		showLocation("Chocolates For Breakfast", "15+Tamariki+Avenue,+Orewa,+Auckland", true);
	});
	$('td.chgOfAddrss').click(function() {
		showLocation("Change Of Address", "52+Edinburgh+Street,+Pukekohe,+Auckland", true);
	});		
	$('td.bridalGal').click(function() {
		showLocation("Bridal Gallery", "501+Victoria+Street,+Hamilton", false);
	});		
	$('td.vanillaBrd').click(function() {
		showLocation("Vanilla Bridal", "721+Cameron+Road,+Tauranga", true);
	});
    $('td.weddingGwnsDirect').click(function() {
	showLocation("Wedding Gowns Direct", "829+West+Coast+Road,+Oratia,+Auckland", false);
    });
}
