﻿
var $ratingUrl = '/Ratings_Json.aspx';
var $activeTip = null;
var $monthSwitch = false;
var $currMonth = 0;

function galleryItemFinish(item) {
    alert('item');
}

function closeWindow(obj) {
    $(obj).parent().fadeOut();
}

function tipClose() {
    //if ($activeTip != null) {
    //    $activeTip.qtip('hide');
    //}
    //obj.qtip("api").hide();
    $activeTip.qtip('hide');
}

function toolTipShow(ev) {
    $activeTip = $(ev.target);
}

$(document).ready(function() {
    $('.star-rating, .rating-cancel').bind('click', function() {
        ratingChanged($(this), $(this).children('a:first').text());
    });
});

$(function() {

    //$('#d-enewsSignup').find('input.input').focus(function() {
    //    $(this).css('background', '#fff url(/Sites/Common/Images/splash/signup_blank.gif) no-repeat 0 0');
    //});

    $('.tagCloudClass ul>li').tsort({ attr: 'value' });
    $('.tagCloudClass ul').tagcloud({ type: 'list', height: 145, power: .5 });
});

function ratingChanged(obj, val) {
    if (val == '') {
        val = '-1';
    }
    if (val > 0) {
        $curr = obj.parent().fadeTo('slow', 0.3);
    }
    else {
        $curr = obj.parent().fadeTo('slow', 0.3);
    }
    var dataPackage = 'rating=' + val;
    if (jQuery.browser.mozilla || jQuery.browser.msie) {
        dataPackage = dataPackage + '&content=' + $curr.parent().parent().attr('rel');
    }
    else {
        dataPackage = dataPackage + '&content=' + $curr.parent().parent().attr('rel');
    }
    $.ajax({ url: $ratingUrl, data: dataPackage, success: ratingCallback, type: 'POST', dataType: 'JSON', error: ratingCallback });
}

function ratingCallback(d, stat) {
    if (stat == 'success') {
    }
    if (stat == 'error') {
        alert('an error has occurred. Please wait a few minutes and try again.\n\n');
        $('.d-itemRating').hide();
        $curr.children('.rating-cancel a').click();
    }
    if ($curr != null) {
        $curr.fadeTo('slow', 1.0);
    }
}

$(document).ready(function() {

    $(function() {

        $(".trueTabs").tabs();

        $(".digIt").click(function() {
            var parentPos = $(this).parent().offset();
            var pos = $(this).offset();
            var left = pos.left - parentPos.left - $(this).parent().find('.d-itemRating').width();
            $(this).parent().find('.d-itemRating').css('margin-left', left + 'px');
            if (jQuery.msie) {
                $(this).parent().find('.d-itemRating').corners('30');
            }
            $(this).parent().find('.d-itemRating').toggle();
            return false;
        });

    });

    $(function() {

        var $tabs = $(".contentTabs").tabs({ fx: { opacity: 'toggle'} });
        var tabCount = 0;

        $(".contentTabs .ui-tabs-panel").each(function(i) {

            var totalSize = $(".contentTabs .ui-tabs-panel").size() - 1;

            if (i != totalSize) {
                next = i + 1;
                $(this).append("<a href='#' class='next-tab mover' rel='" + next + "'>&#187;</a>");
            }

            if (i > 0) {
                prev = i - 1;
                $(this).append("<a href='#' class='prev-tab mover' rel='" + prev + "'>&#171;</a>");
            }
            tabCount++;
        });

        $('.next-tab, .prev-tab').click(function() {
            $tabs.tabs('select', $(this).attr("rel") * 1);
            return false;
        });

        var currentTab = 0;

        

    });

    $(function() {

        var $data = null;

        var protocol = jQuery.url.attr("protocol");
        var host = jQuery.url.attr("host");

        $.getJSON(protocol + '://' + host + '/cultureclock/Events_Json.aspx',
                      function(data) {
                          $data = data;
                          $('#thisMonth').datePicker({ inline: true,
                              hoverCallback: function($td, thisDate, month, year) {
                                  alert('hover');
                              },
                              renderCallback: function($td, thisDate, month, year) {
                                  var clear = false;
				  var bound = false;
                                  $.each($data, function(i, item) {
                                      var currDate = new Date(thisDate);
				      var renderMonth = currDate.getMonth();
                                      if (currDate.getDate() >= item.Date.getDate() && currDate.getDate() <= item.EndDate.getDate()
                                            && currDate.getMonth() >= item.Date.getMonth() && currDate.getMonth() <= item.EndDate.getMonth()
                                            && currDate.getYear() >= item.Date.getYear() && currDate.getYear() <= item.EndDate.getYear()) {
                                          var contentId = 'd-' + currDate.getYear() + currDate.getMonth() + currDate.getDate();
                                          var dateDisplay = currDate.toLocaleDateString();
                                          var $contentContainer = null;
					  if (month != $currMonth) {
						$('#calendarContent').html('');
					  }
                                          if ($('#calendarContent').find('#' + contentId).length == 0) {
                                              clear = true;
                                              $('#calendarContent').append('<div class="eventItemContainer" id="' + contentId + '" style="z-index:1000; display:none; position: absolute;"><div style="width:100%;"><span class="eventDateDisplay">' + dateDisplay + '</span><img style="float:right;cursor:hand;" src="/Sites/SunsetMag/Images/tipClose.gif" alt="CLOSE" class="tipCloser" onclick="tipClose()" /></div><div class="eventItemContent"></div></div>');
                                              $contentContainer = $('#calendarContent').find('#' + contentId).find('.eventItemContent');
                                              $td.qtip({
                                                  content: $('#calendarContent').find('#' + contentId),
                                                  show: 'click',
                                                  hide: 'click',
                                                  position: { corner: { target: 'topLeft', tooltip: 'bottomRight'} },
                                                  style: {
                                                      width: 400,
                                                      height: 253,
                                                      border: { width: 1, radius: 5, color: '#cccccc' },
                                                      tip: 'bottomRight'
                                                  },
                                                  api: { onShow: toolTipShow }
                                              });
					      bound = true;
                                              //$('#calendarContent').find('#' + contentId).find('.eventItemContent').append('<div class="eventItem"><strong>' + item.Title + '</strong><br />' + item.Location + '<br/>' + item.BriefDescription + '<br/><a target="_blank" href="' + (item.Link == '' ? '#' : item.Link) + '">view</a>');
                                          }
                                          else {
                                              $contentContainer = $('#calendarContent').find('#' + contentId).find('.eventItemContent');
					      if (!bound) {
	                                              $td.qtip({
        	                                          content: $('#calendarContent').find('#' + contentId),
                	                                  show: 'click',
                        	                          hide: 'click',
                                	                  position: { corner: { target: 'topLeft', tooltip: 'bottomRight'} },
                                        	          style: {
                                                	      width: 400,
	                                                      height: 253,
        	                                              border: { width: 1, radius: 5, color: '#cccccc' },
                	                                      tip: 'bottomRight'
                        	                          },
                                	                  api: { onShow: toolTipShow }
                                        	      });
						      bound = true;
					      }

                                              //$('#calendarContent').find('#' + contentId).find('.eventItemContent').append('<div class="eventItem"><strong>' + item.Title + '</strong><br />' + item.Location + '<br/>' + item.BriefDescription + '<br/><a target="_blank" href="' + (item.Link == '' ? '#' : item.Link) + '">view</a>');
                                          }
                                          if ($contentContainer != null) {
                                              $contentContainer.append('<div class="eventItem"><strong>' + item.Title + '</strong><br />' + item.Location + '<br/>' + item.BriefDescription + '<br/><a id="' + contentId + '" target="_blank" href="' + (item.Link == '' ? '#' : item.Link) + '">view</a>');
                                          }
                                          $td.css('background-color', '#7e0626');
					  $currMonth = month;
                                      }
                                  });
                              }
                          }).bind('dpMonthChanged', function(ev, m, y) { $monthSwitch = true;  });
                      });
    });

});
