/*
* @author Rades Dan (dan.rades@gmail.com)
*/
App = {
    overlayElm : null,
    winElm : null,
    cache : {},
    username : '',
    initialise : function()
    {
	if (typeof pageName != 'undefined' && typeof this.initors[pageName] == 'function')
	{
	    App.initors[pageName]();
	}
	this.overlayElm = $('#overlay');
	this.initNewsletter();
	this.initConcurs();
	this.initSearch();
	var xproducts = $.cookie('xproducts') || 0;
	var html = (xproducts == 1) ? '<b>1</b> produs in cos' : '<b>' + xproducts + '</b> produse in cos';
	$('#xproducts').html(html);
    },

    initSearch : function()
    {
	var searchFrm = $('#searchFrm');
	$('input.input_caut', searchFrm).click(function()
	{
	    if (this.value == 'cauta') this.value = '';
	});
		
	$('a',searchFrm).click(function()
	{
	    searchFrm.submit();
	    return false;
	});
	searchFrm.submit(function()
	{
	    if ($('input.input_caut', searchFrm).val().length < 3)
	    {
		alert('Introdu minim 3 caractere pentru cautare!');
		return false;
	    }
	});
    },
	
    initNewsletter : function()
    {
		$('#newsletter_subscribe_btn').click(function() {
		    /*
				var name = $('#newsletter_subscribe input')
								emailVal = email.val();
								var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;							
								if(reg.test(emailVal) == false) 
								{
									emailErr.show();
									return false;
								
				//$('#newsletter_subscribe').submit
				*/
		    $.post('/subscribe', $('#newsletter_subscribe').serializeArray(), function()
		    {
			alert('Iti multumim pentru inscrierea la newsletter!');
		    });
		});
    },
	initConcurs : function()
    {
		$('#concurs_subscribe_btn').click(function() {
		    /*
				var name = $('#newsletter_subscribe input')
								emailVal = email.val();
								var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;							
								if(reg.test(emailVal) == false) 
								{
									emailErr.show();
									return false;
								
				//$('#newsletter_subscribe').submit
				*/
		    $.post('/subscribe_concurs', $('#concurs_subscribe').serializeArray(), function()
		    {
				alert('Felicitari! Te-ai inscris in concurs. \n Daca esti fericitul castigator, te vom contacta la finalul perioadei de promotie.');
		    });
		});
    },
    overlay : function(show)
    {
	var display = (show == 'show') ? 'block' : 'none';
	this.overlayElm.css({
	    'height' : $(document).height(),
	    'opacity' : 0.7,
	    'display' : display
	});
	this.overlayElm.bgiframe();
    },

    repositionWin : function(w)
    {
	if (typeof w == 'object')
	{
	    var top = (($(window).height() / 2) - (w.outerHeight() / 2)) - 75;
	    top = top + $(window).scrollTop();
	    var left = (($(window).width() / 2) - (w.outerWidth() / 2));
						
	    if( top < 0 ) top = 0;
	    if( left < 0 ) left = 0;
			
	    w.css({
		top: top + 'px',
		left: left + 'px'
	    });
	}
    },


    windowMaintainPosition : function(win, status)
    {
	var maintain = function(){
	    App.repositionWin(_win);
	}
	switch(status)
	{
	    case true:
		$(window).bind('resize', maintain);
		if( $.browser.msie)
		{
		    $(window).bind('scroll', maintain);
		}
		break;
	    case false:
		$(window).unbind('resize', onresize);
		if( $.browser.msie)
		{
		    $(window).unbind('scroll', maintain);
		}
		break;
	}
    },

    rand : function( min, max )
    {
	return Math.floor(Math.random() * (max - min + 1)) + min;
    }
}

App.initors = {
    'homepage' : function()
    {
    },
	
    'product' : function()
    {
	var jqZoomObj = {
	    zoomWidth : 365,
	    zoomHeight : 283,
	    position : 'left',
	    xOffset : 5,
	    yOffset : -15,
	    title : false
	}
	
	var jqZoomElm = {};

	o = {
	    imageBtnNext : '/images/lightbox-btn-next.gif',
	    imageBtnPrev : '/images/lightbox-btn-prev.gif',
	    imageBtnClose : '/images/lightbox-btn-close.gif',
	    imageLoading : '/images/lightbox-ico-loading.gif',
	    imageBlank : '/images/lightbox-blank.gif',
	    txtImage : 'Imaginea',
	    txtOf : 'din'
	};
	$('a.lightbox').lightBox(o);
	var mainImages = $('.main_images');
	var thumbs = $('#productImages li');
	thumbs.each(function(i)
	{
	    $(this).click(function() {
		mainImages.hide();
		var current = mainImages.eq(i);
		current.show();
		if (typeof jqZoomElm[i] == 'undefined')
		{
		    $('.imagezoom', current).jqzoom(jqZoomObj);
		    jqZoomElm[i] = 1;
		}
		thumbs.removeClass('selected');
		$(this).addClass('selected');
		return false;
	    });
	});
		
	thumbs.eq(0).click();
    },

    'distributie' : function() {
	ContactMap.initialize();
    }
};


var ContactMap = {};
ContactMap.markers = [
{
    lat : '44.43715',
    lng : '26.12133',
    data : {
	name : 'BUS',
	adress : 'Str. Traian 98',
	phone : '07222giant; 0722.244.268'
    }
},
{
    lat : '44.48176',
    lng : '26.09212',
    data : {
	name : 'Hypersport 1',
	adress : 'Sos. N. Caranfil nr.34',
	phone : '0727.477.678'
    }
},
{
    lat : '46.56963',
    lng : '26.91245',
    data : {
	name : 'Bike &amp; Sport',
	adress : 'Bulevardul Unirii nr.2',
	phone : '0734.998.206'
    }
},
{
    lat : '44.44251',
    lng : '26.16772',
    data : {
	name : 'ASSport 1',
	adress : 'Sos. Pantelimon nr. 94',
	phone : '021.250.35.28'
    }
},
{
    lat : '44.44188',
    lng : '26.11753',
    data : {
	name : 'ASSport 2',
	adress : 'Calea Mosilor nr. 46',
	phone : '021.311.39.03'
    }
},
{
    lat : '44.48176',
    lng : '26.09212',
    data : {
	name : 'Hypersport 1',
	adress : 'Sos. N. Caranfil nr.34',
	phone : '0727.477.678'
    }
},
{
    lat : '44.44221',
    lng : '26.14098',
    data : {
	name : 'Veloteca',
	adress : 'Sos. Iancului nr. 100',
	phone : '021 311.82.69'
    }
},
{
    lat : '44.42169',
    lng : '26.09555',
    data : {
	name : 'Bicishop',
	adress : 'Str. 11 IUNIE nr. 73',
	phone : '0726.663.877'
    }
},
{
    lat : '44.41554',
    lng : '26.10315',
    data : {
	name : 'Probike',
	adress : 'Str. Lanariei nr 93-95',
	phone : '0755.010.899'
    }
},
{
    lat : '44.44435',
    lng : '26.11972',
    data : {
	name : 'High Sports',
	adress : 'Calea Mosilor nr.122',
	phone : '0749.272.467'
    }
},
{
    lat : '44.43992',
    lng : '26.16744',
    data : {
	name : 'Hala cu Biciclete',
	adress : 'Sos. Morarilor nr.2',
	phone : '0742.342.444'
    }
},
{
    lat : '44.94538',
    lng : '26.01279',
    data : {
	name : 'Emmeduesport',
	adress : 'B-dul Republicii, nr 151',
	phone : '0721.275.636'
    }
},
{
    lat : '45.786604',
    lng : '24.14883',
    data : {
	name : 'Freetime',
	adress : 'Str. Lomonosov  nr. 1',
	phone : '0269.245.871'
    }
},
{
    lat : '46.76180',
    lng : '23.56780',
    data : {
	name : 'BikeSport',
	adress : 'Calea Manastur nr. 105',
	phone : '0744.263.649'
    }
},
{
    lat : '44.84528',
    lng : '24.88884',
    data : {
	name : 'Rock Bike',
	adress : 'Str. Fratii Golesti 29.E',
	phone : '0733.938.323'
    }
},
{
    lat : '45.77812',
    lng : '21.22078',
    data : {
	name : 'Par Alpin',
	adress : 'Calea Aradului nr. 25',
	phone : '0356.440.858'
    }
}

];
ContactMap.map = null;
ContactMap.bubble = null;
ContactMap.htmlCache = {};

ContactMap.createMarker = function(i) {
    var marker = new GMarker(new GLatLng(ContactMap.markers[i].lat, ContactMap.markers[i].lng), {});
    GEvent.addListener(marker, 'click', function() {

	//console.log(markerData);
	//console.log($('#bubble').html());
	marker.openInfoWindowHtml(ContactMap.htmlCache[i]);
    });
    return marker;
}

ContactMap.initialize = function()
{
    if (GBrowserIsCompatible())
    {
	var uiOptions = new GMapUIOptions(new GSize({
	    width:680,
	    height: 440
	}));
	uiOptions.controls.smallzoomcontrol3d = true;
	uiOptions.controls.menumaptypecontrol = true;

	this.map = new GMap2(document.getElementById("map_canvas"));
	this.map.setCenter(new GLatLng(44.4313, 26.1032), 11);

	this.map.setUI(uiOptions);
	this.map.setMapType(G_NORMAL_MAP);
	for (i = 0; i < ContactMap.markers.length; i++) {
	    $('#shop').html(ContactMap.markers[i].data.name);
	    $('#adress').html(ContactMap.markers[i].data.adress);
	    $('#phone').html(ContactMap.markers[i].data.phone);
	    this.htmlCache[i] = $('#bubble').html();
	}
	for (i = 0; i < ContactMap.markers.length; i++) {
	    this.map.addOverlay(this.createMarker(i));
	}

    }
}

$(document).ready(function() 
{
    //$(document).pngFix();
    App.initialise();
	
});	

function fbs_click(u, t) {
    u=u || location.href;
    t=t || document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}
	
	

