(function($)
{
	
  // Here is the entry point for your front javascript
  $('input[placeholder]').placeholder();


  if ((template != undefined) && (items != undefined)){

	$(document).ready(function(){
		
		
		// Instanciate controller
		gui = new GuiController(template, items);
 		

		//the onItemActivated event fires AFTER an item is activated and animation is complete
		//_me.currentItem references the item just having been activated
		gui.addEventListener("onItemActivated",function(_me) {
			_me.showCaption(_me.currentItem);
		});

		// the onItemDeactivated event is fired before an item is deactivated
		//_me.currentItem references the item about to be deactivated
		gui.addEventListener("onItemDeactivated",function(_me) {

			// Hide the caption
			_me.hideCaption(_me.currentItem);

			if(_me.currentItem.type == 'video') {
				// Stop and hide the video player
				if(player) {
					player.setStop();
					$("#player").hide();
				}
				_me.hideCaption(_me.currentItem);
			}
			else if(_me.currentItem.type == 'article') {
				// Hide article here
				$("#article").hide();
			}
			else if(_me.currentItem.type == 'vimeo') {
				// Stop (for performance reasons) and position video
				$("#vimeo").empty();
				$("#vimeo").hide();
			}
			
		});


		// the onClickCurrentItem event is fired when a user clicks an active item
		//_me.currentItem references the clicked item
		gui.addEventListener("onClickCurrentItem",function(_me) {
			if (_me.currentItem.type == 'link') {
                window.location = _me.currentItem.link_url;
            }
			if(_me.currentItem.type == 'article') {
                window.location = _me.currentItem.article_url;
			}
            else if(_me.currentItem.type == 'video') {
				
				// Initialize player if needed
				if(!player) {
					player = projekktor('#player');
				}

				// Position video player
				$("#player").css('width',_me.template.body.active.width);
				$("#player").css('height',_me.template.body.height);
				$("#player").css('left',_me.currentItem.jqRef.position().left);

				$("#player").show();

				// Set video to play, formats for different browsers
				playlist = [{
					0:{src:'projekktor/'+ _me.currentItem.videoName +'.mp4', type: 'video/mp4'},
					1:{src:'projekktor/'+ _me.currentItem.videoName +'.ogv', type: 'video/ogg'},
					2:{src:'projekktor/'+ _me.currentItem.videoName +'.webm', type: 'video/mp4'}
				}];
				player.setFile(playlist);

				// Set preview image
				player.setPlayerPoster(_me.currentItem.url);
				player.setActiveItem('poster');
			}
			else if(_me.currentItem.type == 'vimeo') {
                $("#vimeo").css('width',gui.template.body.active.width).css('height',gui.template.body.height);
                $("#vimeo").css('left',gui.template.body.inactive.width + gui.template.margin.horizontal).css('top',gui.template.header.height);
                $("#vimeo").empty();
				$("#vimeo").show();
				console.log("SHOW!");
				$("#vimeo").oembed(_me.currentItem.vimeo_url,{vimeo: {autoplay: true, portrait: false}},function(container, oembed) {
					$("#vimeo").append(oembed.code);
					$("#vimeo iframe").css('width',"100%");
					$("#vimeo iframe").css('height',"100%");
				});
			}

			
		});
		

		// onResize event fired after all resizing is done
		gui.addEventListener("onResize",function(_me) {
			
			if(_me.currentItem) {
				if(_me.currentItem.type == 'article') {
					// Position the article
					$("#article").css('width',_me.template.body.active.width);
					$("#article").css('height',_me.template.body.height + _me.template.lower.height + _me.template.margin.vertical);
					$("#article").css('left',_me.currentItem.jqRef.position().left);
				}
				else if(_me.currentItem.type == 'video') {
					// Stop (for performance reasons) and position video
					player.setStop();
                    $("#player").css('width',gui.template.body.active.width).css('height',gui.template.body.height);
                    $("#player").css('left',gui.template.body.inactive.width + gui.template.margin.horizontal).css('top',gui.template.header.height);
					//$("#player").hide();
				}
				else if(_me.currentItem.type == 'vimeo' || $("#vimeo").css('display') == 'block') {
                    $("#vimeo").css('width',gui.template.body.active.width).css('height',gui.template.body.height);
                    $("#vimeo").css('left',gui.template.body.inactive.width + gui.template.margin.horizontal).css('top',gui.template.header.height);
				}
			}
			
		});
		

		// All set up, initialize the controller!
		gui.init();


	});
  }

    if (carousels) { 
        $(document).ready(function(){
            for(i in carousels) {
                config = carousels[i].config;
                obj = $(carousels[i].id);
                obj.jCarouselLite(config);
            }
        });
    }

    //setup bikes selector menu
    $(document).ready(function(){
        $('#topmenu ul.greybox li').each(function(index,value){
            $(this).mouseover(function(){
                model = $(this).find('a').attr('id').replace(/bikelink-/,'');
                if (model){
                    $(this).parent().find('li.active').removeClass('active');
                    $(this).addClass('active');
                    $('#topmenu .col_3 div.bikemenu-item').filter(':visible').css('display','none');
                    $('#bikemenu-'+model).css('display','block');
                }
            });
            $(this).click(function(){
                window.location = $(this).find('a').attr('href');
            });
        });
    });

    function show_feature_video(el){
        video_url = $(el.currentTarget).find('a').attr('href');
        gui.hideButtons();
        $("#videoblocker").show();
        $("#vimeo").css('width',gui.template.body.active.width).css('height',gui.template.body.height);
        $("#vimeo").css('left',gui.template.body.inactive.width + gui.template.margin.horizontal).css('top',gui.template.header.height);
        $("#vimeo").empty();
        $("#vimeo").show();
        $("#vimeo").oembed(video_url,{vimeo: {autoplay: true, portrait: false}},function(container, oembed) {
            $("#vimeo").append(oembed.code);
            $("#vimeo iframe").css('width',"100%");
            $("#vimeo iframe").css('height',"100%");
        });

        return false;
    }

    //setup video features
    $(document).ready(function(){
        $('#video_scroller li').each(function(index,value){
            $(this).click(show_feature_video);
        });
        $('#videoblocker').click(function(obj){
            $(this).hide();
            $('#vimeo').empty();
            $('#vimeo').hide();
            gui.showButtons();
            obj.stopPropagation();
         });
    });

    if (dealerpage){

        var updateDealerList = function(){
            territory = $('#territory-selector div.active').first().attr('id').replace(/subpagebutton-.-/,'');

            if ($('#country-selector div.active').length) {
                country = $('#country-selector div.active').first().attr('id').replace(/subpagebutton-.-/,'');
            } else if ($('select#country').length) {
                country = $('select#country').val();
            } else {
                country = '';
            }

            if ($('#region-selector div.active').length) {
                region = $('#region-selector div.active').first().attr('id').replace(/subpagebutton-.-/,'');
            } else if ($('select#region').length) {
                region = $('select#region').val();
            } else {
                region = '';
            }

            if (updating_territory) {
                country = '';
                region = '';
                updating_territory = false;
                $('#country-selector').fadeOut(250);
                $('#region-selector').fadeOut(250);
            } else if (updating_country) {
                region = '';
                updating_country = false;
                $('#region-selector').fadeOut(250);
            }

            url_info = $('#dealer-url').val();
            $('#dealer-list-wrap').fadeOut(250, function() { $('#dealer-list-loading').delay(800).fadeIn(250); });
            $('#dealer-area').load('/+/dmWidget/render?' + dealerurl, { ajax: 1, country: country, region: region, territory: territory}, function(){assignDealerHandlers(); $('#dealer-list-wrap').fadeIn(250); });
        };

        var assignDealerHandlers = function(){
            $('div.dealer-selector>div').each(function(index,value){
                $(this).click(function(){
                    subpage = $(this).attr('id').replace(/subpagebutton-/,'');
                    if (!$(this).hasClass('active')){
                        $(this).siblings().removeClass('active');
                        $(this).addClass('active');
                    
                        if (subpage.charAt(0) == 't') {
                            updating_territory = true;
                        } else if (subpage.charAt(0) == 'c') {
                            updating_country = true;
                        }
                
                        updateDealerList();
                    }
                });
            });
            $('div#dealer-area select').each(function(index,value){
                $(this).change(function(){
                    if ($(this).attr('id') == 'territory') {
                        updating_territory = true;
                    }
                    else if ($(this).attr('id') == 'country')
                    {
                        updating_country = true;
                    }

                    updateDealerList();
                });
            });

        };

        $(document).ready(function(){
            assignDealerHandlers();
            if ($('select#country')){
                if ($('select#country').val() != $('#country_value').val()) { updateDealerList(); }
            }
            if ($('select#region')){
                if ($('select#region').val() != $('#region_value').val()) { updateDealerList(); }
            }
            
        });
    }

    //setup bikes tabs
    if (bikestabs){
        $(document).ready(function(){

            $('div.colors-list div.color').each(function(index,value){
                if (!$(this).hasClass('faker')){
                    $(this).mouseover(function(){
                        if ($(this).find('div.color_label').is(':hidden')){
                            $(this).siblings().find('div.color_label').filter(':visible').hide(300);
                            els = $(this).siblings().find('div.color_label').filter(':animated');
                            els.hide(100);
                            
                            $(this).find('div.color_label').show(200);
                        } else {
                            els = $(this).siblings().find('div.color_label').filter(':visible');
                            els.hide(300);
                        }
                    });
                }
            });
            $('div.colors-list div.color').first().mouseover();

            $('div.subpage-selector>div').each(function(index,value){
                $(this).click(function(){
                    subpage = $(this).attr('id').replace(/subpagebutton-/,'');
                    if (!$(this).hasClass('active')){
                        //hide the currently visible one(s)
                        $('div#subpage-' + subpage).siblings().filter(':visible').each(function(){
                            $(this).animate({ 'left': -800, opacity: 0}, 300, function() {
                                $(this).css('display','none'); 
                            });
                        });
                        $('div#subpage-' + subpage).css('left', '800px').css('opacity', 0).css('display', 'block');
                        $('div#subpage-' + subpage).animate({ 'left': 0, opacity: 1}, 300);
                        $(this).siblings().removeClass('active');
                        $(this).addClass('active');
                    }
                });
            });

            $('div#bikeinfo-content .evenstripe table tr').filter(':even').addClass('alt');
            $('div#bikeinfo-content .oddstripe table tr').filter(':odd').addClass('alt');

            $('div.bikemenu ul li').each(function(index,value){
                $(this).click(function(){
                    section = $(this).attr('id').replace(/bikelink-/,'');
                    if (section){
                        if ($(this).hasClass('active')){
                            $(this).parent().find('li.active').removeClass('active');
                            $('#bikeinfo-pane').css('overflow','hidden');
                            $('#bikeinfo-area').animate({ top: 0, height: 0 }, 300);
                            $('#bikeinfo-content>div').filter(':visible').hide();
                        }
                        else{
                            if ($(this).parent().find('li.active').length){
                                $(this).parent().find('li.active').removeClass('active');
                                $(this).addClass('active');
                                viz = $('#bikeinfo-content>div').filter(':visible');
                                if (viz.length)
                                {
                                    $('#bikeinfo-pane').css('overflow','hidden');
                                    viz.animate({left: -500, opacity: 0}, 200, 
                                        function(){ 
                                            viz.css('left', 0).css('display', 'none').css('opacity', 1);
                                            $('#bikeinfo-' + section).css('opacity', 0).css('display', 'block').css('left', 500);
                                            $('#bikeinfo-' + section).animate({left: 0, opacity: 1},200, function(){
                                                $('#bikeinfo-pane').css('overflow-y','auto');
                                            }); 
                                        }
                                    );
                                }
                                else
                                {
                                    $('#bikeinfo-' + section).fadeIn(200);
                                }
                            }
                            else {
                                $(this).addClass('active');
                                $('#bikeinfo-' + section).show();
                                areaheight = Math.min(gui.template.body.height, Math.max(Math.round(gui.template.body.height * 0.8), 450));
                                $('#bikeinfo-area').show();
                                $('#bikeinfo-area').animate({ top: (-1 * areaheight), height: (areaheight - 8)}, 200, function(){
                                    $('#bikeinfo-pane').css('overflow-y','auto');
                                });
                            }
                        }
                    }
                });
            });

            $('#scrollerwrap').click(function(){
                if ($('#bikeinfo-area').is(':visible'))
                {
                    $('div.bikemenu ul li.active').removeClass('active');
                    $('#bikeinfo-pane').css('overflow','hidden');
                    $('#bikeinfo-area').animate({ top: 0, height: 0 }, 150);
                    $('#bikeinfo-content>div').filter(':visible').hide();

                    return false;
                }
                return true;
            });

    		gui.addEventListener("onResize",function(_me) {
                if ($('#bikeinfo-content>div').filter(':visible').length){
                    areaheight = Math.min(gui.template.body.height, Math.max(Math.round(gui.template.body.height * 0.8), 450));
                    $('div#bikeinfo-area div#bikeinfo-content').css('height', (areaheight - 8) + 'px');
                    $('#bikeinfo-area').css('height', areaheight + 'px').css('top', (-1 * areaheight) + 'px');
                }
            });

        });
    }

})(jQuery);

