/*  Author:
    TAMER AYDIN - http://tamerayd.in
*/

function setPlaceholder(id) {
    if (id.val()=="" && id.attr("placeholder")!=""){
        id.val(id.attr("placeholder"));
        id.focus(function(){
            if (id.val()==id.attr("placeholder"))
                id.val("");
        });
        id.blur(function(){
            if (id.val()=="")
                id.val(id.attr("placeholder"));
        });
    }
}

function disableImgDrag(e) { //disable image dragging
    e.preventDefault();
}

window.onload = function (e) {
    var evt = e || window.event, imgs, i;
    if (evt.preventDefault) {
        imgs = document.getElementsByTagName('img');
        for (i = 0; i < imgs.length; i++) {
            imgs[i].onmousedown = disableImgDrag;
        }
    }
};

SALON = {
    msie : false,
    webkit : false,
    ff : false,
    idevice : false, //iphone or ipad
    build : function () {
        if ($.browser.msie) {SALON.msie=true}
        if ($.browser.webkit) {SALON.webkit=true}
        if ($.browser.mozilla) {SALON.ff=true}
        if ((navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPod") != -1) || (navigator.platform.indexOf("iPad") != -1)) {SALON.idevice=true}
        if (!Modernizr.input.placeholder) {
            //setPlaceholder($('#contactName'));
        }
        $('.scroll-pane').jScrollPane({verticalDragMaxHeight:'12'});
        SALON.tweets.init();
        SALON.archive.init();
		SALON.gallery.init();
		SALON.popup.init();
        $('#archiveOrder .list').jScrollPane();
        setTimeout(SALON.archive.categories.order.closeBox,500);
    },
    tweets : {
        count : 0,
        currentTweetIndex : -1,
        timer : 0,
        init : function () {
            $('.tweetList').tweets({username:"saloniksv"});
            setTimeout(function () {
                SALON.tweets.count = $('ul.tweetList').children('li').size();
                if (SALON.tweets.count > 0) {
                    SALON.tweets.nextTweet();
                }
                if (SALON.tweets.count > 1) {
                    SALON.tweets.timer = setInterval(function() {SALON.tweets.nextTweet();}, 8000);
                }
            },2000);
        },
        nextTweet : function () {
            if (SALON.tweets.currentTweetIndex < SALON.tweets.count-1) {
                SALON.tweets.currentTweetIndex++;
            } else {
                SALON.tweets.currentTweetIndex = 0;
            }
            $('#salonTweets > div > a.tweetLink').fadeOut();
            $('#salonTweets > div > p').fadeOut(function () {
                $(this).html($('.tweetList li:eq('+SALON.tweets.currentTweetIndex+')').html());
                $(this).siblings('a.tweetLink').attr('href',$(this).children('input.tweetLink').val()).html($(this).children('input.tweetDate').val());
                $('#salonTweets > div > p, #salonTweets > div > a.tweetLink').fadeIn();
            });
        }
    },
    mobileInfo : function () {
        if ($('.iksvMobile').hasClass('active')) {
            $('.iksvMobile').removeClass('active');
            $('.iksvMobileInfo').hide();
        } else {
            $('.iksvMobile').addClass('active');
            $('.iksvMobileInfo').show();
        }
    },
    search :  {
        onFocus: function () {
            $('#searchKeyword').bind('keyup',SALON.search.onKeyUp);
        },
        onBlur: function () {
            $('#searchKeyword').unbind('keyup',SALON.search.onKeyUp);
        },
        onKeyUp : function (e) {
            var kcode = (e.keyCode ? e.keyCode : e.which);
            if (kcode!=27 && $('#searchKeyword').val().length > 2) {
                SALON.search.showSuggestionBox($('#searchKeyword').val());
            } else {
                SALON.search.closeSuggestionBox();
            }
        },
        selectSuggestion : function () {
            $('#searchKeyword').val($(this).html());
            SALON.search.closeSuggestionBox();
            setTimeout(function () {$('#searchForm').submit();},200)
        },
        showSuggestionBox : function (hint) {
            
            //AJAX
            //hint e gore #searchSuggestionList icerini guncelliyoruz...
            $.get('/search/index', {hint:hint}, function(response){
				$('#searchSuggestionList').html(response);
				$('#searchSuggestion').show();
	            $('#searchSuggestion > ul > li').live('click',SALON.search.selectSuggestion);
			}, 'html');
			
        },
        closeSuggestionBox : function () {
            $('#searchSuggestion').hide();
            $('#searchSuggestion > ul > li').die('click',SALON.search.selectSuggestion);
        },
        filter : function () {
            $('#searchSuggestion .filter').removeClass('selected');
            $(this).addClass('selected');
            switch ($(this).attr('id')) {
                case 'searchFilterArtist' : $('#searchSuggestion > ul > li.genre').hide(); $('#searchSuggestion > ul > li.artist').show(); break;
                case 'searchFilterGenre' : $('#searchSuggestion > ul > li.artist').hide(); $('#searchSuggestion > ul > li.genre').show(); break;
                default : $('#searchSuggestion > ul > li.artist, #searchSuggestion > ul > li.genre').show(); break;
            }
        }
    },
    archive : {
        pageCount : 0,
        currentPage : 1,
        detailMode : 0,
        init : function () {
            SALON.archive.pageCount = $('.archiveContainer > div.archiveList').children('div').size();
            $('.archiveContainer > div.archiveList').css('width',(920*SALON.archive.pageCount)+'px');
            if (SALON.archive.pageCount < 2)
                $('.archiveNavigation').hide();
            else 
                $('.archiveNavigation').show();
        },
        navigate : function (rotation) {            
            if (!$('.archiveNavigation').hasClass('detail')) {
                if (!$(this).hasClass('disabled')) {
                    switch (rotation) {
                        case 'prev':
                            if (SALON.archive.currentPage != 1) {
                                SALON.archive.currentPage = SALON.archive.currentPage - 1;
                            }
                            break;
                        default:
                            if (SALON.archive.currentPage != SALON.archive.pageCount) {
                                SALON.archive.currentPage = SALON.archive.currentPage + 1;
                            }
                            break;
                    }
                    $('.archiveContainer > div.archiveList').animate({'margin-left':'-'+((SALON.archive.currentPage-1)*920)+'px'},'fast','linear');
                    $('.archiveNavigation').removeClass('disabled');
                    if (SALON.archive.currentPage == 1) {
                        $('.archiveNavigation.prev').addClass('disabled');
                    } else if (SALON.archive.currentPage == SALON.archive.pageCount) {
                        $('.archiveNavigation.next').addClass('disabled');
                    }
                }
            } else {
                $('#archiveLoader').fadeIn(function(){
	
					var a = null;

					switch (rotation) {
	                    case 'prev':
							a = $('#event_detail_nav_prev');
	                        break;
	                    default:
							a = $('#event_detail_nav_next');
	                        break;
	                }

					if(a != null)
					{
						History.pushState(a.attr('rel'), a.attr('title'), a.attr('href'));

		                $.get(a.attr('href'), {pushnav:1}, function(response){

							$('#archiveDetail').html(response);
							bindPushState();
							$('.scroll-pane').jScrollPane({verticalDragMaxHeight:'12'});
							SALON.share.init();
							
							setTimeout(function () {$('#archiveLoader').fadeOut('slow');},500);

						}, 'html');
					}
				
				
				});
				
				
            }
        },
        showDetail : function (href, css) {
			
            if (SALON.archive.detailMode==0) {
                $('.archiveCategories').hide();
                $('#archiveLoader, .archiveNavigation').show();
                $('.archiveContainer > div.archiveDetail').animate({'margin-top':'0px'},'fast','linear');
                $('.archiveNavigation').addClass('detail');
                SALON.archive.detailMode=1;

				//AJAX...
	            // yukleme tamamlandiginda -> $('#archiveLoader').fadeOut('slow');

	            //gecici

				if(href != null)
				{
					$.get(href, {pushnav:1, css:css}, function(response){

						$('#archiveDetail').html(response);
						bindPushState();
						$('.scroll-pane').jScrollPane({verticalDragMaxHeight:'12'});
						SALON.share.init();
						SALON.popup.init();
						setTimeout(function () {$('#archiveLoader').fadeOut('slow');},500);

					}, 'html');
				}
				else
				{
					$('#archiveLoader').hide();
				}
            } else {
                $('#archiveLoader').fadeIn(function(){
					//AJAX...
		            // yukleme tamamlandiginda -> $('#archiveLoader').fadeOut('slow');

		            //gecici

					if(href != null)
					{
						$.get(href, {pushnav:1, css:css}, function(response){

							$('#archiveDetail').html(response);
							bindPushState();
							$('.scroll-pane').jScrollPane({verticalDragMaxHeight:'12'});
							SALON.share.init();
							setTimeout(function () {$('#archiveLoader').fadeOut('slow');},500);

						}, 'html');
					}
					else
					{
						$('#archiveLoader').hide();
					}
				});
            }
            
            
			
            
        },
        closeDetail : function () {
            //$('#archiveDetail').html('');
            $('.archiveContainer > div.archiveDetail').animate({'margin-top':'-960px'},'fast','linear');
            $('.archiveNavigation').removeClass('detail');
            if (SALON.archive.pageCount < 2)
                $('.archiveNavigation').hide();
            $('.archiveCategories').show();
            SALON.archive.detailMode=0;
        },
        sponsors : {
            slideRight : function () {
                var gallery = $('.galleryContainer > div');
                if (gallery.width() > 390) {
                    var diff = gallery.width() - 390;
                    gallery.animate({'margin-left':'-'+diff+'px'},'fast','linear');
                }
            },
            slideLeft : function () {
                var gallery = $('.galleryContainer > div');
                if (gallery.width() > 390) {
                    gallery.animate({'margin-left':'0px'},'fast','linear');
                }
            }
        },
        categories : {
            filter : function (){
                $('.archiveNavigation').hide();
                $('.archiveContainer').prepend('<div id="archiveFilterLoader"><div class="loaderIcon filter"></div></div>');
                if ($(this).hasClass('selected'))
                    $(this).removeClass('selected');
                else
                    $(this).addClass('selected');
                var activeFilters = '';
                $('#archiveFilter > a').each(function () {
                    if ($(this).hasClass('selected'))
                        activeFilters = activeFilters + $(this).attr('rel') + ' ';
                });
                
                //gecici
                
				$.get('/' + language + '/event/filter', {f:activeFilters}, function(response){

					$('.archiveList').html(response);
					bindPushState();
					setTimeout(function () {$('#archiveFilterLoader').fadeOut('slow',function() {$(this).remove();SALON.archive.init();});},500);
					

				}, 'html');
                
                //AJAX...
                // yukleme tamamlandiginda -> $('#archiveFilterLoader').fadeOut('slow',function() {$(this).remove();SALON.archive.init();});
            },
            order : {
                opened : 0,
                openBox : function () {
                    if (SALON.archive.categories.order.opened == 0) {
                        SALON.archive.categories.order.opened = 1;
                        $(document).bind('keypress', SALON.archive.categories.order.onKeyUp);
                        $('#archiveOrder .list').show();
                    } else {
                        SALON.archive.categories.order.closeBox();
                    }
                },
                closeBox : function () {
                    SALON.archive.categories.order.opened = 0;
                    $(document).unbind('keypress', SALON.archive.categories.order.onKeyUp);
                    $('#archiveOrder .list').hide();
                },
                onKeyUp : function (e) {
                    var kcode = (e.keyCode ? e.keyCode : e.which);
                    if (kcode==27)
                        SALON.archive.categories.order.closeBox();
                },
                select : function () {
                    if (!$(this).hasClass('selected')) {
                        $('#archiveOrder li').removeClass('selected');
                        $(this).addClass('selected');
                        $('#archiveOrder .select').html($(this).text());
                        $('.archiveNavigation').hide();
                        $('.archiveContainer').prepend('<div id="archiveFilterLoader"><div class="loaderIcon filter"></div></div>');

                        //gecici
						$.get('/' + language + '/event/archive', {d:$(this).attr('rel')}, function(response){

							$('.archiveList').html(response);
							bindPushState();
							setTimeout(function () {$('#archiveFilterLoader').fadeOut('slow',function() {$(this).remove();SALON.archive.init();});},500);


						}, 'html');

                        //AJAX...
                        // yukleme tamamlandiginda -> $('#archiveFilterLoader').fadeOut('slow',function() {$(this).remove();SALON.archive.init();});
                    }
                    SALON.archive.categories.order.closeBox();
                }
            }
        }
    },
	share : {
	    active : 0,
		init: function(){
			$('.share').bind('mouseover',SALON.share.showList);
		    $('.share').bind('mouseout',SALON.share.hideList);
		    $('#shareList').unbind().bind('mouseover',SALON.share.onList);
		    $('#shareList').unbind().bind('mouseout',SALON.share.offList);
		},
        showList : function () {
            //$(this).prepend('<div id="shareList"><ul><li><a href="'+$(this).attr('rel')+'">e-posta</a></li><li><a href="http://twitter.com/share?url='+$(this).attr('rel')+'">twitter</a></li><li><a href="http://www.facebook.com/sharer.php?u='+$(this).attr('rel')+'">facebook</a></li></ul></div>');
			$(this).prepend('<div id="shareList"><ul><li><a target="_blank" href="http://twitter.com/share?url='+encodeURIComponent($(this).attr('rel'))+'&text=' + encodeURIComponent($('meta[name=description]').attr("content")) + '">twitter</a></li><li><a target="_blank" href="http://www.facebook.com/sharer.php?u='+$(this).attr('rel')+'">facebook</a></li></ul></div>');
        },
        hideList : function () {
            setTimeout(function () {
                if (SALON.share.active != 1)
                    $('#shareList').remove();
            },500);
        },
        onList : function () {
            SALON.share.active = 1;
        },
        offList : function () {
            SALON.share.active = 0;
            $('#shareList').remove();
        }
    },
    gallery : {
        disabled : 0,
        init : function () {
            $('.gallery .galleryContainer').each(function () {
                if ($(this).children('ul').children('li').size()<6)
                    $(this).siblings('.galleryNavigation').children('a').remove();
            });
        },
        navigate : function () {
            if (SALON.gallery.disabled == 0) {
                SALON.gallery.disabled = 1;
                var nav = $(this).parent('.galleryNavigation');
                var ul = nav.siblings('.galleryContainer').children('ul');
                var childCount = ul.children('li').size();
                var leftM = ul.margin().left;
                if (nav.hasClass('prev')) {
                    if (leftM==0)
                        ul.animate({'margin-left':leftM-((childCount-5)*140)},'fast',function () {SALON.gallery.disabled = 0;});
                    else
                        ul.animate({'margin-left':leftM+140},'fast',function () {SALON.gallery.disabled = 0;});
                } else {
                    if (leftM==-(childCount-5)*140)
                        ul.animate({'margin-left':'0px'},'fast',function () {SALON.gallery.disabled = 0;});
                    else
                        ul.animate({'margin-left':leftM-140},'fast',function () {SALON.gallery.disabled = 0;});
                }
            }
        }
    },
	popup : {
		init : function(){
			$('.galleryContainer a').bind('click', SALON.popup.open);
			$('a.photo').not('.nodata').bind('click', SALON.popup.open_photos);
		},
	    open : function () {
			$('#popupList').html('');
			$(this).parent().parent().find('li a').each(function(){$(this).clone().appendTo('#popupList');});
			$('#popupList a').unbind().bind('click', SALON.popup.load);
			
			$('.popupObject h1 span').html($(this).attr('title'));
			
			if($(this).attr('rel') == 'photo')
			{
				$('.popupObject img').attr('src', $(this).attr('href'));
			}
			
            $('#popupContainer').css('min-height',$('.contentContainer').height());
            $('#popupContainer').show();

			return false;
        },
        open_photos : function () {
			$('#popupList').html('');
			$('#event_photos ul li a').each(function(){$(this).clone().appendTo('#popupList');});
			$('#popupList a').unbind().bind('click', SALON.popup.load);
			
			$('.popupObject h1 span').html($(this).attr('title'));		
			
			$('.popupObject img').attr('src', $('#event_photos ul li a:first').attr('href'));
			$('.button.archiveNavigation.prev').hide();
			$('.button.archiveNavigation.next').hide();
			
            $('#popupContainer').css('min-height',$('.contentContainer').height());
            $('#popupContainer').show();

			return false;
        },
		load: function(){
			
			if($(this).attr('rel') == 'photo')
			{
				$('.popupObject img').attr('src', $(this).attr('href'));
			}
			
			return false;
			
		},
        close : function () {
            $('#popupContainer').hide();
            $('.button.archiveNavigation.prev').show();
			$('.button.archiveNavigation.next').show();
        }
    }

}

$(document).ready(function() {
    
    SALON.build();
    
    //IKSV MOBILE BOX
    $('.iksvMobile,.iksvMobileClose').bind('click',SALON.mobileInfo);
    
    //SEARCH
    $('#searchKeyword').bind('focus',SALON.search.onFocus);
    $('#searchKeyword').bind('blur',SALON.search.onBlur);
    $('#searchSuggestion .filter').bind('click',SALON.search.filter);
    $(document).keypress(function(e) {
        var kcode = (e.keyCode ? e.keyCode : e.which);
        if (kcode==27) {
            SALON.search.closeSuggestionBox();
        }
    });
    
    //ARCHIVE
    $('.archiveNavigation.next').bind('click',function () {SALON.archive.navigate('next');});
    $('.archiveNavigation.prev').bind('click',function () {SALON.archive.navigate('prev');});
    $('#archiveFilter > a').bind('click',SALON.archive.categories.filter);

	//SHARE
    SALON.share.init();
    
    //ORDER/FILTER
    $('#archiveOrder .select').bind('click',SALON.archive.categories.order.openBox);
    $('#archiveOrder .list li').bind('click',SALON.archive.categories.order.select);

	//GALLERY
    $('.galleryNavigation.next > a').bind('click',SALON.gallery.navigate);
    $('.galleryNavigation.prev > a').bind('click',SALON.gallery.navigate);
    
});

/* push state navigation */

var goPushState = function(){
	var a = $(this);
	
	History.pushState(a.attr('rel'), a.attr('title'), a.attr('href'));
	
	if(a.attr('rel') == 'event_detail')
	{
		var x = a.parent();
		while(!x.hasClass('colorcode'))
		{
			x = x.parent();
		}
		
		var css = x.attr('class').replace('colorcode code-', '');
		SALON.archive.showDetail(a.attr('href'), css);
	}
	else if(a.attr('rel') == 'home')
	{
		SALON.archive.closeDetail();
	}
	
	return false;
}

var bindPushState = function(){
	
	$('a.pushnav').unbind('click', goPushState).bind('click', goPushState);
}

$().ready(function(){
	bindPushState();
});
