//Extends jQuery+
jQuery.fn.extend({
	exists:function(){return $(this).size()>0},
	trim:function(){$(this).val(jQuery.trim($(this).val()))},
	ajaxResponse:function(_url, funct) {
		var aj=$.ajax({
			url: _url,
			async:true,
			complete: function(obj, go) {
				if (go=="success") {
					$("div.image").html(obj.responseText);
					if (funct) funct();
				}
			},
			dataType: "text",
			type: "get"
		});
	}

})
//End Extends jQuery

var Search={
		home:{
			init:function(){
				$("#allOffer input.reload").remove();
				//$("#allOffer select").css("width","15em");
			}
		},
		offer:{
			init:function(){
				$("#offerSearch input.reload").remove();
				//$("#offerSearch select").css("width","15em");
			}
		},
		shop:{
			init:function(){
				$("#shopSearch input.reload").remove();
				//$("#shopSearch select").css("width","15em");
			}
		},
		search:{
			init:function(){
				//$("form#searchOffers").addClass("hayJS");
				$("#searchOffers input.reload").css("display","none");
				//$("#searchOffers select").css("width","15em");
			}
		},
		frm_localizador:{
			init:function(){
				//$("#frm_localizador").addClass("hayJS");
				$("#frm_localizador input.reload").remove();
				//$("#frm_localizador select").css("width","15em");
			}
		}
}

var ModOffers={
		show_per_page:4, //how much items per page to show
		number_of_items: 0, //getting the amount of elements inside content div
		number_of_pages: 0, //calculate the number of pages we are going to have,
		current_page:0,
		init:function(){

			$(".mod-catalogo").each(function(e){
/*
				$(this).append("<div class=\"pagination\"></div>")
					   .find(".pagination").append("<span class=\"pages\">Página <span class=\"actual\">1</span> de <span class=\"total\">1</span></span>")
										   .append("<ul></ul>")
					   					   .find("ul").append("<li class=\"prev\"><a href=\"...\" title=\"Página anterior\"><img src=\"/ibcomv3/webpartner/images/ico_arrows_left.gif\" alt=\"\" /> Anterior</a></li>")
						   							  .append("<li class=\"next\"><a href=\"...\" title=\"Página siguiente\">Siguiente <img src=\"/ibcomv3/webpartner/images/ico_arrows_right.gif\" alt=\"\" /></a></li>")
*/
				ModOffers.number_of_items=$(this).find("ul.list-offer li").size();
				ModOffers.number_of_pages=Math.ceil(ModOffers.number_of_items/ModOffers.show_per_page);
				$(this).find(".pagination span.pages span.total").html(ModOffers.number_of_pages)
				var obj=$(this);
				$(this).find(".pagination ul li.prev a").click(function(){
					ModOffers.prevPage(obj);
					return false;
				})
				$(this).find(".pagination ul li.next a").click(function(){
					ModOffers.nextPage(obj);
					return false;
				})

				//Hide Link to 'Previous' Page
				$(this).find(".pagination ul li.prev").css("display","none");
					//Hide Link to 'Next' Page if only exists one page
					if(ModOffers.number_of_pages==1)
						$(this).find(".pagination ul li.next").css("display","none");

			})

		},
		prevPage:function(obj){

			new_page = parseInt($(obj).find(".pagination span.pages span.actual").html()) - 1;


			//if there is an item before the current active link run the function

			if($(obj).find(".pagination span.pages span.actual").html()!=1){
				$(obj).find(".pagination span.pages span.actual").html(new_page)
				ModOffers.goPage(new_page-1,obj);
			}else{
				alert('no more pages inf');
			}
		},
		nextPage:function(obj){
			new_page = parseInt($(obj).find(".pagination span.pages span.actual").html());


			//ModOffers.goPage(new_page,obj);
			//if there is an item after the current active link run the function
			if($(obj).find(".pagination span.pages span.actual").html()!=$(obj).find(".pagination span.pages span.total").html()){
				$(obj).find(".pagination span.pages span.actual").html(new_page+1)
				ModOffers.goPage(new_page,obj);
			}else{
				alert('no more pages sup');
			}
		},
		goPage:function(page,obj){
			//get the element number where to start the slice from
			start_from = page*ModOffers.show_per_page;

			//get the element number where to end the slice
			end_on = start_from + ModOffers.show_per_page;

			//hide all children elements of content div, get specific items and show them
			$(obj).find("ul.list-offer li").css('display', 'none').slice(start_from, end_on).css('display', 'block');

			//hide or show 'Next Page' link
			if((page+1)==parseInt($(obj).find(".pagination span.pages span.total").html())){
				$(obj).find(".pagination ul li.next").css("display","none");
			}else{
				$(obj).find(".pagination ul li.next").css("display","block");
			}

			//hide or show 'Prev Page' link
			if(page==0){
				$(obj).find(".pagination ul li.prev").css("display","none");
			}else{
				$(obj).find(".pagination ul li.prev").css("display","block");
			}
		}
}


var ListItems={
		init:function(){
			$(".listItems ul li").each(function(){
				$(this).find("img").css("opacity","0.5")
				$(this).mouseover(function(){
					$(this).css("backgroundImage","url('/ibcomv3/webpartner/images/mini_picture_frame_or.png')")
					$(this).css("backgroundRepeat","no-repeat")
					$(this).find("img").css("opacity","1")
				}).mouseout(function(){
					$(this).css("backgroundImage","none")
					$(this).find("img").css("opacity","0.5")
				})
			})
		}
}

var preSlideViewer={                                                                                                                                                                                                                                                           
	path:"/ibcomv3/webpartner/images_cnt/",                                                                                                                                                                                                                        
	arrayImgs:new Array(),                                                                                                                                                                                                                                         
	lnElem:0,                                                                                                                                                                                                                                                      
	init:function(){                                                                                                                                                                                                                                               
		var container=$(".svw");                                                                                                                                                                                                                               
		container.wrap('<div id="container-viewer"></div>');                                                                                                                                                                                                   
			preSlideViewer.lnElem=ImgsBanner.length;                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                           
			container.find("ul").remove();                                                                                                                                                                                                                 
			$(".svw").append('<ul></ul>');  
			
			 for(var i=0;i<preSlideViewer.lnElem;i++){                                                                                                                                                                                                     
					if((ImgsBanner[i].imgHrefTarget==undefined)||(ImgsBanner[i].imgHrefTarget=="")){     
						$(".svw ul").append('<li><a href="'+ImgsBanner[i].imgHref+'" title=""><img src="'+preSlideViewer.path+ImgsBanner[i].imgSrc+'" alt="" width="600" height="160" /></a></li>');
						
					}else{    
						$(".svw ul").append('<li><a href="'+ImgsBanner[i].imgHref+'" title="" target="'+ImgsBanner[i].imgHrefTarget+'"><img src="'+preSlideViewer.path+ImgsBanner[i].imgSrc+'" alt="" width="600" height="160" /></a></li>');
					}                                                                                                                                                                                                                              
					/*$(".svw ul").append('<li><a href="_'+ImgsBanner[i].imgHref+'" title=""><img src="'+preSlideViewer.path+ImgsBanner[i].imgSrc+'" alt="" width="598" height="160" /></a></li>');*/                                              
				}                                                                                                                                                                                                                                      
		}                                                                                                                                                                                                                                                      
}                                  

var OrderSubItems={
		speed:200,
		elmFirst:new Array(), //[0]--> Link URL || [1]--> Image SRC
		elmClicked:new Array(),
		animate:false,
		init:function(){
			//$("div.trademark:not(:first)").css("opacity",0.5);

			$("#detailItem .item").each(function(){
				var objParent=$(this);
				objParent.find("a.goUp").remove();
				objParent.find("div.trademark").each(function(e){if(e!=0){$(this).addClass("hide");}})
				objParent.find("ul.subitems li:first").addClass("active");
				objParent.find("ul.subitems li").each(function(e){
					//Set opacity default
					$(this).css("opacity",0.5);
					var objChild=$(this);

					$(this).mouseover(function(){
						if($.browser.msie && ($.browser.version=='7.0')){
							objChild.css("backgroundImage","url('/ibcomv3/webpartner/images/mini_picture_frame_or_bl.gif')")
						}else{
							objChild.css("backgroundImage","url('/ibcomv3/webpartner/images/mini_picture_frame_or.png')")
						}

						$(this).css("backgroundRepeat","no-repeat")
						$(this).css("opacity","1")

					}).mouseout(function(){
						if($.browser.msie && ($.browser.version=='7.0')){
							objChild.css("backgroundImage","url('/ibcomv3/webpartner/images/mini_picture_frame_bl.gif')")
						}else{
							objChild.css("backgroundImage","url('/ibcomv3/webpartner/images/mini_picture_frame_bl.png')")
						}

						$(this).css("opacity","0.5")
					})


					$(this).find("a").click(function(){
					if(OrderSubItems.animate) return false;
						if(e!=0){
							OrderSubItems.animate=true;
							OrderSubItems.elmFirst[0]=objParent.find("ul.subitems li:first a").attr("href");
							OrderSubItems.elmFirst[1]=objParent.find("ul.subitems li:first a img").attr("src");
							OrderSubItems.fadeOut($(this).parent()); //Pass li clicked
							OrderSubItems.showTrademark($(this).closest("div.item"),$(this).attr("href"))
						}
						return false;
					})
				})
				//Set opacity 1 to first element
				objParent.find("ul.subitems li:first").css("opacity",1);
			})




		},
		fadeOut:function(elm){
			elm.parent().find("li:first").animate({opacity:0},OrderSubItems.speed)
			//$("ul.subitems li:first").animate({opacity:0},OrderSubItems.speed)
			elm.animate({opacity:0},OrderSubItems.speed,function(){
				OrderSubItems.elmClicked[0]=elm.find("a").attr("href");
				OrderSubItems.elmClicked[1]=elm.find("a").find("img").attr("src");
				OrderSubItems.interChange(elm)
			})
		},
		fadeIn:function(elm){
			elm.parent().find("li:first").animate({opacity:1},{duration:OrderSubItems.speed, complete:function(){OrderSubItems.animate=false;}});
			//$("ul.subitems li:first").animate({opacity:1},OrderSubItems.speed);
			elm.animate({opacity:0.5},OrderSubItems.speed);
		},
		interChange:function(elm){
			//Interchange images partners
			//First to Clicked
			elm.find("a").attr("href",OrderSubItems.elmFirst[0]);
			elm.find("a").find("img").attr("src",OrderSubItems.elmFirst[1]);
			//Clicked to First
			elm.parent().find("li:first a").attr("href",OrderSubItems.elmClicked[0]);
			elm.parent().find("li:first a img").attr("src",OrderSubItems.elmClicked[1]);
			//$("ul.subitems li:first a").attr("href",OrderSubItems.elmClicked[0]);
			//$("ul.subitems li:first a img").attr("src",OrderSubItems.elmClicked[1]);
			OrderSubItems.fadeIn(elm)
		},
		showTrademark:function(elmClosest,href){
			var _idElm=href.substr(1)
			elmClosest.find("div.trademark").hide();
			$("div#"+_idElm).show().removeClass("hide");
		}
}

var ResultSearch={
		animateSpeed:400,
		init:function(){

			$("#result-search .promo div.detail-promo").css("display","none");
			$("#result-search .promo").each(function(e){
				var obj=$(this);
				//Init Show/Hide Obtencion/Utilizacion Offer
				obj.find("ul.view-offers li a").click(function(){
					var cls=$(this).parent().attr("class")
					var objDiv=obj.find("div.detail-promo."+cls);
					objDiv.css("display","block")
					objDiv.find("span.close a").click(function(){
						objDiv.css("display","none");
						return false;
					})

					return false;
				})
				//End Show/Hide Obtencion/Utilizacion Offer
				//Init Show/Hide More/Less Description
				var _height=obj.find("div.description").height();
				obj.find("div.description").css({"overflow":"hidden","height":"30px"}).addClass("short").after('<a href="..." title="Despliega descripción completa" class="toggle">[+]</a>')

				obj.find("a.toggle").click(function(){
					if(obj.find("div.description").hasClass("short")){
						$(this).parent().find("div.description").removeClass("short").animate({"height":_height},ResultSearch.animateSpeed,"linear",function(){
							$(this).parent().find("a.toggle").text("[-]")
						})
					}else{
						$(this).parent().find("div.description").animate({"height":"30px"},ResultSearch.animateSpeed,"linear",function(){
							$(this).parent().find("a.toggle").text("[+]")
							$(this).parent().find("div.description").addClass("short")
						})

					}
					return false;
				})
				//End Show/Hide More/Less Description
			})
		}
}

var Viewmore={
	animateSpeed:400,
	animation:false,
	str:{
		obtencion:{
			mostrar:"Mostrar ofertas de obtencion",
			ocultar:"Ocultar ofertas de obtencion"
		},
		utilizacion:{
			mostrar:"Mostrar ofertas de utilizacion",
			ocultar:"Ocultar ofertas de utilizacion"
		}
	},
	imgSrc:{
		mostrar:"/ibcomv3/webpartner/images/ico_arrows_bottom.gif",
		ocultar:"/ibcomv3/webpartner/images/ico_arrows_top.gif"
	},
	load:function(){
		$(".mostrarOcultar .viewmore").each(Viewmore.init)
	},
	init:function(){
		if($(this).hasClass("obtencion")){
			$(this).html(Viewmore.str.obtencion.mostrar)
		}else{
			$(this).html(Viewmore.str.utilizacion.mostrar)
		}
		$(this).addClass("mostrar").wrapInner("<a href='#'/>").click(Viewmore.toggle)
				.append(' <img src="'+Viewmore.imgSrc.mostrar+'" alt="" />')
		$(this).next("div").hide()
	},
	toggle:function(){
		if(!Viewmore.animation){
			Viewmore.animation=true;
			$(this).next("div").slideToggle(Viewmore.animateSpeed,function(){
				var link = $(this).prev(".viewmore")
				if(link.hasClass("mostrar")){
					//Cambiar texto
					if( link.hasClass("obtencion") ){
						link.find("a").html(Viewmore.str.obtencion.ocultar)
					}else{
						link.find("a").html(Viewmore.str.utilizacion.ocultar)
					}
					//Cambiar imagen
					link.find("img").attr("src", Viewmore.imgSrc.ocultar)

				}else{
					//Cambiar texto
					if( link.hasClass("obtencion") ){
						link.find("a").html(Viewmore.str.obtencion.mostrar)
					}else{
						link.find("a").html(Viewmore.str.utilizacion.mostrar)
					}
					//Cambiar imagen
					link.find("img").attr("src", Viewmore.imgSrc.mostrar)
				}
				link.toggleClass("mostrar")
				Viewmore.animation=false;
			})
		}
		return false;

	}
}




$(document).ready(function(){
	if($(".flash-map").exists()){$(".flash-map").find("a").remove();} //Remove img map before replace with Flash
	if($("#allOffer").exists()){Search.home.init();}
	if($("#offerSearch").exists()){Search.offer.init();}
	if($("#shopSearch").exists()){Search.shop.init();}
	if($("#searchOffers").exists()){Search.search.init();}
	if($("#frm_localizador").exists()){Search.frm_localizador.init();}
	if($(".mod-catalogo ul.list-offer").exists()){ModOffers.init();}
	if($(".listItems").exists()){ListItems.init();}
	if($("ul.subitems").exists()){OrderSubItems.init()}
	if($("#result-search").exists()){ResultSearch.init();}
	if($(".mostrarOcultar .viewmore").exists()){Viewmore.load();}

})


	// 	BUSQUEDA FLASH POBLACION
	function buscarEstablecimiento(isoPoblacion, codPostal, isoSector, idPartner, isoMapa, isoPais) {
		frm = document.getElementById('frm_localizador_mapa');

		// Inicializamos los campos
		frm.isoMapa.value = "";
		frm.isoPoblacion.value = "";
		frm.codPostal.value = "";
		frm.isoSector.value = "";
		frm.idPartnerLocalizador.value = "";
		frm.isoPais.value = "";

		// Obtenemos el valor del mapa
		if ( isoMapa != null && isoMapa != "undefined" ) {
			frm.isoMapa.value = isoMapa;
		}
		if ( isoPoblacion != null && isoPoblacion != "undefined" ) {
			frm.isoPoblacion.value = isoPoblacion;
		}
		if ( codPostal != null && codPostal != "undefined" ) {
			frm.codPostal.value = codPostal;
		}
		if ( isoSector != null && isoSector != "undefined" ) {
			frm.isoSector.value = isoSector;
		}
		if ( idPartner != null && idPartner != "undefined" ) {
			frm.idPartnerLocalizador.value = idPartner;
		}
		if ( isoPais != null && isoPais != "undefined" ) {
			if ( isNaN(isoPais) )
				frm.isoPais.value = isoPais;
			else
				frm.isoPais.value = isoPais * 1;
		}

		redirect('frm_localizador_mapa');
	}

//-->


