/*------------------------------------
	Theme Name: Coupons
---------------------------------------*/
/*	
	
	* Document Scroll		
		
	* Document Ready
		- Responsive Caret
		- Scrolling Navigation
		- Add Easing Effect
		- Search
		- CountDown
	* Window Load
		- Site Loader
*/

(function($) {

	"use strict"

	/* - Responsive Caret* */
	function menu_dropdown_open(){
		var width = $(window).width();
		if( width > 991 ) {
			if($(".ow-navigation .nav li.ddl-active").length ) {
				$(".ow-navigation .nav > li").removeClass("ddl-active");
				$(".ow-navigation .nav li .dropdown-menu").removeAttr("style");
			}
		} else {
			$(".ow-navigation .nav li .dropdown-menu").removeAttr("style");
		}
	}
	
	/* - Expand Panel Resize * */
	function panel_resize(){
		var width = $(window).width();
		if( width > 991 ) {
			if($(".header-section #slidepanel").length ) {
				$(".header-section #slidepanel").removeAttr("style");
			}
		}
	}
	
	function sticky_menu() {
		var menu_scroll = $('header[class*="header_s"]').offset().top;
		var scroll_top = $(window).scrollTop();
		
		if ( scroll_top > menu_scroll ) {
			$(".header_s.menusticky .ownavigation").addClass("navbar-fixed-top animated fadeInDown");
		} else {
			$(".header_s.menusticky .ownavigation").removeClass("navbar-fixed-top animated fadeInDown"); 
		}
	}
	
	
	function header_menu() {
		var width	=	$(window).width();
		if ( width >= 992 ) {
			if( $(".rev_slider").length == 1 ) {
				$(".header_s1 .ownavigation").addClass("nav_absolute");
			}
			else {
				$(".header_s1 .ownavigation").removeClass("nav_absolute");
			}			
		}
		else {
		}
	};
	
	$('.catagories-section .catagories-box .catagories-img-box').on('click touchend', function(e) {
		var el = $(this);
	});
	
	/* ## Document Ready - Handler for .ready() called */
	$(document).ready(function($) {

		/* - Scrolling Navigation* */
		var width	=	$(window).width();
		var height	=	$(window).height();
		
		/* - Set Sticky Menu* */
		if( $(".menusticky").length ) {
			sticky_menu();
		}
		
		$('.navbar-nav li a[href*="#"]:not([href="#"]), .site-logo a[href*="#"]:not([href="#"])').on("click", function(e) {
	
			var $anchor = $(this);
			
			$("html, body").stop().animate({ scrollTop: $($anchor.attr("href")).offset().top - 49 }, 1500, "easeInOutExpo");
			
			e.preventDefault();
		});

		/* - Responsive Caret* */
		$(".ddl-switch").on("click", function() {
			var li = $(this).parent();
			if ( li.hasClass("ddl-active") || li.find(".ddl-active").length !== 0 || li.find(".dropdown-menu").is(":visible") ) {
				li.removeClass("ddl-active");
				li.children().find(".ddl-active").removeClass("ddl-active");
				li.children(".dropdown-menu").slideUp();
			}
			else {
				li.addClass("ddl-active");
				li.children(".dropdown-menu").slideDown();
			}
		});
		
		/* - Expand Panel * */
		$("#slideit").on ("click", function() {
			$("#slidepanel").slideDown(1000);
			$("html").animate({ scrollTop: 0 }, 1000);
		});	

		/* Collapse Panel * */
		$("#closeit").on("click", function() {
			$("#slidepanel").slideUp("slow");
			$("html").animate({ scrollTop: 0 }, 1000);
		});	
		
		/* Switch buttons from "Log In | Register" to "Close Panel" on click * */
		$("#toggle a").on("click", function() {
			$("#toggle a").toggle();
		});	
		
		panel_resize();
		
		/* - Search* */
		if($(".search-box").length){
			$("#search").on("click", function(){
				$(".search-box").addClass("active")
			});
			$(".search-box > span").on("click", function(){
				$(".search-box").removeClass("active")
			});
		}
		
		/* - CountDown */		
		var ele_id = 0;
		$( "[id*='clock-']" ).each(function () { 
			ele_id = $(this).attr("id").split("-")[1];
			var cnt_date = $(this).attr("data-date");
			$("[id*='clock-"+ele_id+"']").countdown(cnt_date, function(event) {
				var $this = $(this).html(event.strftime(''    
				+ '<p>%D <span>d.</span></p>'
				+ '<p>%H <span>h.</span></p>'
				+ '<p>%M <span>m.</span></p>'
				+ '<p>%S <span>s.</span></p>'));
		    });
		});

		/* - DatePicker */
		if($(".coupon-submit").length){
			$( "#datepicker" ).datepicker();
		}
		
		/* - Select Box */
		$( "select:not(.wpcf7-form-control)" ).wrap( "<div class='select_box'></div>" );
		
		header_menu();

		$('.get_coupon_code').on("click",function() {
			var elem_id = $(this).attr('id');
			var coupon_code = $(this).data('text');

			if( coupon_code ) {
				$("#"+elem_id).zclip({
					path: templateUrl + '/assets/js/ZeroClipboard.swf',
					copy: coupon_code,
					
					afterCopy: function() {
						console.log('copied');
						alert('Code Copy ! Now you can paste it somewhere');
					}
				});
				
			}
		});
		
	});	/* - Document Ready /- */
	
	/* Event - Window Scroll */
	$(window).on("scroll",function() {
		/* - Set Sticky Menu* */
		if( $(".header_s .ownavigation").length ) {
			sticky_menu();
		}
	});
	
	$( window ).on("resize",function() {
		/* - Expand Panel Resize */
		panel_resize();
		
		var width	=	$(window).width();
		var height	=	$(window).height();
		
		header_menu();
		
	});
	
	/* ## Window Load - Handler for .load() called */
	$(window).on("load",function() {
		/* - Site Loader* */
		if ( !$("html").is(".ie6, .ie7, .ie8") ) {
			$("#site-loader").delay(1000).fadeOut("slow");
		}
		else {
			$("#site-loader").css("display","none");
		}
		header_menu();
	});
	
	/* - Social Share */
	if( $('.social-icons').length  ) {
		$('ul.social-icons li > a', this).on('click', function() {

			var data_action = $(this).attr('data-action');
			var data_title = $(this).attr('data-title');
			var data_url = $(this).attr('data-url');

			if( data_action == 'facebook' ) {
				window.open('http://www.facebook.com/share.php?u='+encodeURIComponent(data_url)+'&title='+encodeURIComponent(data_title),'sharer','toolbar=0,status=0,width=580,height=325');
			}
			else if( data_action == 'twitter' ) {
				window.open('http://twitter.com/intent/tweet?status='+encodeURIComponent(data_url)+'+'+encodeURIComponent(data_title),'sharer','toolbar=0,status=0,width=580,height=325');
			}
			else if( data_action == 'google-plus' ) {
				window.open('https://plus.google.com/share?url='+encodeURIComponent(data_url),'sharer','toolbar=0,status=0,width=580,height=325');
			}
			else if( data_action == 'linkedin' ) {
				window.open('http://www.linkedin.com/shareArticle?mini=true&url='+encodeURIComponent(data_url)+'&title='+encodeURIComponent(data_title)+'&source='+encodeURIComponent(data_url),'sharer','toolbar=0,status=0,width=580,height=325');
			}
			else if( data_action == 'instagram' ) {
				window.open('http://www.instagram.com/submit?url='+encodeURIComponent(data_url)+'&amp;title='+encodeURIComponent(data_title),'sharer','toolbar=0,status=0,width=580,height=325');
			}
			else if( data_action == 'dribbble' ) {
				window.open('https://dribbble.com/share?url='+encodeURIComponent(data_url),'sharer','toolbar=0,status=0,width=580,height=325');
			}
			else if( data_action == 'pinterest' ) {
				window.open('http://pinterest.com/pin/create/button/?url='+encodeURIComponent(data_url)+'&media=http://cdn2.wpbeginner.com/wp-content/uploads/2013/12/siteground-74x74.jpg&description='+encodeURIComponent(data_title),'sharer','toolbar=0,status=0,width=580,height=325');
			}
		});
	}
})(jQuery);